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

import io.gmss.fiscad.entities.infocad.parametre.Personne;
import io.gmss.fiscad.entities.rfu.metier.CaracteristiqueUniteLogement;
import io.gmss.fiscad.entities.rfu.metier.EnqueteUniteLogement;
import io.gmss.fiscad.paylaods.response.restoration.CaracteristiqueUniteLogementPayLoad;
import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

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


public interface CaracteristiqueUniteLogementRepository extends JpaRepository<CaracteristiqueUniteLogement, Long> {
    @Modifying
    @Transactional
    void deleteAllByEnqueteUniteLogement(EnqueteUniteLogement enqueteUniteLogement);
    Optional<CaracteristiqueUniteLogement> findByMobileDataId(Long id);
    @Modifying
    @Transactional
    @Query("""
    DELETE FROM CaracteristiqueUniteLogement ul
    WHERE ul.enqueteUniteLogement.enquete.id = :enqueteId
    """)
    void deleteByEnqueteId(@Param("enqueteId") Long enqueteId);
    @Query(nativeQuery = true,
            value = "select cul.id as idBackend, " +
                    "cul.external_key as externalKey, " +
                    "c.external_key as caracteristiqueId, " +
                    "eul.external_key as enqueteUniteLogementId, " +
                    "cul.valeur as valeur " +
                    "from caracteristique_unite_logement cul " +
                    "inner join caracteristique c on c.id = cul.caracteristique_id " +
                    "inner join enquete_unite_logement eul on eul.id = cul.enquete_unite_logement_id " +
                    "where cul.terminal_id = ?"
    )
    List<CaracteristiqueUniteLogementPayLoad> getCaracteristiqueUniteLogementByTerminalId(Long terminalId);
}
