package io.gmss.fiscad.repositories.rfu.metier;

import io.gmss.fiscad.entities.infocad.parametre.Personne;
import io.gmss.fiscad.entities.rfu.metier.EnqueteBatiment;
import io.gmss.fiscad.paylaods.response.restoration.EnqueteBatimentPayLoad;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;


public interface EnqueteBatimentRepository extends JpaRepository<EnqueteBatiment, Long> {
    Optional<EnqueteBatiment> findByMobileDataId(Long id);
    Optional<EnqueteBatiment> findFirstByExternalKeyAndTerminal_Id(Long externalKey, Long TerminalId);

    List<EnqueteBatiment> findAllByEnquete_Id(Long enqueteId);

    @Query(
            nativeQuery = true,
            value = "select eb.id as idBackend, " +
                    "eb.terminal_id as terminalId, " +
                    "eb.external_key as externalKey, " +
                    "eb.autre_caracteristique_physique as autreCaracteristiquePhysique, " +
                    "eb.autre_menuisierie as autreMenuisierie, " +
                    "eb.autre_mur as autreMur, " +
                    "eb.date_debut_excemption as dateDebutExcemption, " + //ok
                    "eb.date_fin_excemption as dateFinExcemption, " + //ok
                    "eb.nbre_habitant as nbreHabitant, " + //ok
                    "eb.nbre_lot_unite as nbreLotUnite, " + //ok
                    "eb.nbre_menage as nbreMenage, " + //ok
                    "eb.nbre_mois_location as nbreMoisLocation, " + //ok
                    "eb.num_compteur_sbee as numCompteurSbee, " + //ok
                    "eb.num_compteur_soneb as numCompteurSoneb, " + //ok
                    "eb.sbee as sbee, " + //ok
                    "eb.nbre_unite_location as nbreUniteLocation, " + //ok
                    "eb.soneb as soneb, " + //ok
                    "eb.surface_au_sol as surfaceAuSol, " + //ok
                    "eb.surface_louee as surfaceLouee, " + //ok
                    "eb.valeur_mensuelle_location as valeurMensuelleLocation, " + //ok
                    "b.external_key as batimentId, " + //ok
                    "e.external_key as enquteId, " + //ok
                    "p2.external_key as personeId " + //ok
                    "from enquete_batiment eb " +
                    "inner join batiment b on b.id = eb.batiment_id " +
                    "inner join parcelle p on p.id = b.parcelle_id " +
                    "inner join personne p2 on p2.id = eb.personne_id " +
                    "inner join enquete e on e.id = eb.enquete_id " +
                    "WHERE eb.terminal_id = ?1"
    )
    List<EnqueteBatimentPayLoad> getEnqueteBatimentByTerminalId(Long terminalId);


    void deleteAllByEnquete_Id(Long idEnquete);
}
