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

import io.gmss.fiscad.entities.infocad.metier.Upload;
import io.gmss.fiscad.entities.infocad.parametre.Personne;
import io.gmss.fiscad.entities.rfu.metier.EnqueteBatiment;
import io.gmss.fiscad.entities.rfu.metier.EnqueteUniteLogement;
import io.gmss.fiscad.paylaods.response.restoration.UploadPayLoad;
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 java.util.List;
import java.util.Optional;

public interface UploadRepository extends JpaRepository<Upload, Long> {
    @Modifying
    @Transactional
    void deleteUploadByPiece_Personne_Id(Long personneId);

    //void deleteUploadByPiece_ActeurConcerne_Enquete_Id(Long enqueteId);
    @Query(nativeQuery = true, value = "delete  from upload u where u.id in (select u.id from upload u" +
            "           inner join piece p on p.id = u.piece_id" +
            "           inner join acteur_concerne ac on ac.id = p.acteur_concerne_id" +
            "           inner join enquete e on e.id = ac.enquete_id where e.id = ?1 )")
    @Modifying
    @Transactional
    void deleteUploadByEnqueteIdNatif(Long enqueteId);




    @Modifying
    @Transactional
    void deleteUploadByEnqueteId(Long enqueteId);

    @Modifying
    @Transactional
    void deleteUploadByMembreGroupe_PersonneRepresantee_Id(Long personneRepresente);
    @Modifying
    @Transactional
    void deleteUploadByPersonne_Id(Long personneId);
    Optional<Upload> findByMobileDataId(Long id);
    @Query(value = "Select  " +
            " u.id as idBackend, " +
            " u.external_key as externalKey, " +
            " p.external_key as pieceId, " +
            " u.observation as observation, " +
            " u.membre_groupe_id as membreGroupeId, " +
            " u.name, " +
            " u.file_path as filePath, " +
            " u.synchronise," +
            " u.terminal_id as terminalId, " +
            " u.max_numero_piece_id, " +
            " u.max_numero_acteur_concerne_id, " +
            " u.max_numero_upload_id, " +
            " u.enquete_id as enqueteId, " +
            " u.bloc_id as blocId, " +
            " u.file_name as fileBackendName " +
            " From upload u inner join public.piece p on p.id = u.piece_id" +
            " where u.terminal_id = ?1"
            , nativeQuery = true)
    List<UploadPayLoad> getUploadByTerminalId(Long terminalId);

    @Transactional
    @Modifying
    void deleteAllByEnqueteBatiment(EnqueteBatiment enqueteBatiment);

    void deleteAllByEnqueteUniteLogement(EnqueteUniteLogement enqueteUniteLogement);

@Transactional
@Modifying
    @Query(value = "update upload " +
            " set nombre_parcelle_geom = ?1   " +
            " where id = ?2"
            , nativeQuery = true)
    void majNombreParcelle(int nbreparcelle , Long id);

    /////////////////////////////////
    Optional<Upload> findDistinctFirstByExternalKeyAndTerminal_Id(Long externalKey, Long terminalId);

    List<Upload> findByPersonne_IdAndPieceIsNullAndMembreGroupeIsNull(Long personneId);

}
