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

import io.gmss.fiscad.entities.infocad.metier.MembreGroupe;
import io.gmss.fiscad.entities.infocad.parametre.Personne;
import io.gmss.fiscad.paylaods.response.restoration.MembreGroupePayLoad;
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 MembreGroupeRepository extends JpaRepository<MembreGroupe, Long> {
    Optional<MembreGroupe> findFirstByExternalKeyAndTerminal_Id(Long externalKey, Long terminalId);
    Optional<MembreGroupe> findByMobileDataId(Long id);
    @Modifying
    @Transactional
    void deleteMembreGroupeByPersonneRepresantante_Id(Long personneRepresenteId);

    @Query(value = "Select  " +
            " mg.id as idBackend, " +
            " mg.external_key as externalKey, " +
            " p.external_key as personneSecondaireId, " +
            " p.external_key as personneId, " +
            " mg.position_representation_id as positionRepresentationId, " +
            " mg.type_representation_id as typeRepresentationId, " +
            " mg.enquete_id as enqueteId, " +
            " mg.bloc_id as blocId, " +
            " mg.observation as observation," +
            " mg.synchronise," +
            " mg.terminal_id as terminalId, " +
            " mg.max_numero_piece_id, " +
            " mg.max_numero_acteur_concerne_id " +
            " From membre_groupe mg inner join personne p on p.id = mg.personne_represantante_id and p.id = mg.personne_represantee_id " +
            " where mg.terminal_id = ?1"
            , nativeQuery = true)
    List<MembreGroupePayLoad> getMembreGroupeByTerminalId(Long terminalId);

    List<MembreGroupe> findByPersonneRepresantee_Id(Long id);

}
