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

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

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


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

    List<EnqueteUniteLogement> findAllByEnquete_Id(Long id);

    @Query(nativeQuery = true,
            value = "select eul.id as idBackend, " +
                    "eul.terminal_id as terminalId, " +
                    "eul.external_key as externalKey, " +
                    "eul.surface as surface, " +
                    "eul.nbre_piece as nbrePiece, " +
                    "eul.nbre_habitant as nbreHabitant, " +
                    "eul.nbre_menage as nbreMenage, " +
                    "eul.en_location as enLocation, " +
                    "eul.montant_mensuel_loyer as montantMensuelLoyer, " +
                    "eul.nbre_mois_en_location as nbreMoisEnLocation, " +
                    "eul.montant_locatif_annuel_declare as montantLocatifAnnuelDeclare, " +
                    "eul.surface_louee as surfaceLouee, " +
                    "eul.sbee as sbee, " +
                    "eul.soneb as soneb, " +
                    "eul.num_compteur_sbee as numCompteurSbee, " +
                    "eul.num_compteur_soneb as numCompteurSoneb, " +
                    "eul.date_debut_excemption as dateDebutExcemption, " +
                    "eul.date_fin_excemption as dateFinExcemption, " +
                    "eul.personne_id as personneId, " +
                    "eul.unite_logement_id as uniteLogementId, " +
                    "eul.enquete_id as enqueteId, " +
                    "eul.user_id as userId " +
                    "from enquete_unite_logement eul " +
                    "inner join enquete e on e.id = eul.enquete_id " +
                    "inner join unite_logement ul on eul.unite_logement_id = ul.id " +
                    "inner join personne p on eul.personne_id = p.id " +
                    "where eul.terminal_id = ?1")
    List<EnqueteUniteLogementPayLoad> getEnqueteUniteLogementByTerminalId(Long terminalId);

    void deleteAllByEnquete_id(Long id) ;
}
