package io.gmss.fiscad.repositories.decoupage;

import io.gmss.fiscad.entities.decoupage.Commune;
import io.gmss.fiscad.entities.decoupage.Departement;
import io.gmss.fiscad.paylaods.response.CommuneEnqResponse;
import io.gmss.fiscad.paylaods.response.CommuneSyncResponse;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface CommuneRepository extends JpaRepository<Commune, Long> {

    @Query(value = "select id, code, nom, departement_id as departementid from commune where deleted is false",nativeQuery = true)
    List<CommuneSyncResponse> getCommuneResponse();


    List<Commune> findAllByDepartement(Departement departement);

    @Query(value = " Select  T.id,T.code, T.libelle,T.longitude,T.latitude, sum(case when T.enqueteId is null then 0 else 1 end) as nombreEnquete " +
            " From (select distinct c.id as id,c.code as code, c.nom as libelle, c.longitude,c.latitude, e.id as enqueteId from arrondissement a" +
            " inner join arrondissements_structures sa on sa.arrondissement_id=a.id " +
            " inner join commune c on c.id= a.commune_id " +
            " left join bloc b on b.arrondissement_id=a.id " +
            " left join enquete e on e.bloc_id=b.id " +
            " where sa.structure_id = ?1 )T" +
            " group by T.id,T.code, T.libelle,T.longitude,T.latitude",nativeQuery = true)
    List<CommuneEnqResponse> getCommuneEnqResponse(Long structure_id);


    @Query(value = " Select  T.id,T.code, T.libelle,T.longitude,T.latitude, sum(case when T.enqueteId is null then 0 else 1 end) as nombreEnquete " +
            " From (select distinct c.id as id,c.code as code, c.nom as libelle,c.longitude,c.latitude, e.id as enqueteId from arrondissement a" +
            " left join arrondissements_structures sa on sa.arrondissement_id=a.id " +
            " left join commune c on c.id= a.commune_id " +
            " left join bloc b on b.arrondissement_id=a.id " +
            " left join enquete e on e.bloc_id=b.id)T " +
            " group by T.id,T.code, T.libelle,T.longitude,T.latitude",nativeQuery = true)
    List<CommuneEnqResponse> getAdminCommuneEnqResponse();

}
