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

import io.gmss.fiscad.entities.infocad.parametre.Personne;
import io.gmss.fiscad.entities.rfu.metier.CaracteristiqueBatiment;
import io.gmss.fiscad.entities.rfu.metier.EnqueteBatiment;
import io.gmss.fiscad.paylaods.response.restoration.CaracateristiqueBatimentPayLoads;
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 CaracteristiqueBatimentRepository extends JpaRepository<CaracteristiqueBatiment, Long> {
    @Transactional
    @Modifying
    void deleteAllByEnqueteBatiment(EnqueteBatiment enqueteBatiment);
    Optional<CaracteristiqueBatiment> findByMobileDataId(Long id);
    @Modifying
    @Transactional
    @Query("""
    DELETE FROM CaracteristiqueBatiment cb
    WHERE cb.enqueteBatiment.enquete.id = :enqueteId
    """)
    void deleteByEnqueteId(@Param("enqueteId") Long enqueteId);


    @Query(
            nativeQuery = true,
            value = "select cb.id as idBackend, " +
                    "cb.external_key as externalKey, " +
                    "c.external_key as caracteristiqueId, " +
                    "eb.external_key as enqueteBatimentId, " +
                    "cb.valeur as valeur " +
                    "from caracteristique_batiment cb " +
                    "inner join caracteristique c on cb.caracteristique_id = c.id " +
                    "inner join enquete_batiment eb on eb.id = cb.enquete_batiment_id " +
                    "where cb.terminal_id = ?1"
    )
    List<CaracateristiqueBatimentPayLoads> getCaracteristiqueBatimentByTerminalId(Long terminalId);
}
