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

import io.gmss.fiscad.entities.decoupage.Arrondissement;
import io.gmss.fiscad.entities.infocad.parametre.Bloc;
import io.gmss.fiscad.paylaods.response.BlocEnqResponse;
import io.gmss.fiscad.paylaods.response.BlocSyncResponse;
import io.gmss.fiscad.paylaods.response.report.BlocsParStructureResponse;
import io.gmss.fiscad.paylaods.response.report.ZoneInterventionParStructureResponse;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface BlocRepository extends JpaRepository<Bloc, Long> {
    List<Bloc> findAllByStructure_Id(Long structureId);

    List<Bloc> findAllBySecteur_Id(Long secteurId);

    @Query(value = "select b.id, b.cote, b.nom, b.arrondissement_id as arrondissementid, b.quartier_id as quartierid " +
            " from bloc b" +
            " where b.deleted is false and b.structure_id = ?1 ", nativeQuery = true)
    List<BlocSyncResponse> getBlocResponse(Long structure_id);

    @Query(value = "select b.id, b.cote, b.nom, b.arrondissement_id as arrondissementid, b.quartier_id as quartierid " +
            " from bloc b " +
         //   " inner join secteur_decoupage sd on sd.bloc_id=b.id " +
            " where b.deleted is false and b.structure_id = ?1 and b.secteur_id=?2", nativeQuery = true)
    List<BlocSyncResponse> getBlocSecteurResponse(Long structure_id, Long secteur_id);


    @Query(value = " select b.id, b.cote as code, b.nom as nom , a.nom as arrondissement, c.nom as commune, d.nom as departement " +
            " from bloc b inner join arrondissement a on a.id=b.arrondissement_id" +
            " inner join commune c on c.id=a.commune_id " +
            " inner join departement d on d.id=c.departement_id " +
            " where b.deleted is false and b.structure_id = ?1 ", nativeQuery = true)
    List<BlocsParStructureResponse> getBlocsParStructureResponse(Long structure_id);

    List<Bloc> findAllByArrondissement(Arrondissement arrondissement);

    @Query(value = "select structure_id as id from arrondissements_structures " +
            " where arrondissement_id = ?1" +
            " deleted is false", nativeQuery = true)
    Long getStructureId(Long arrondissement_id);

    @Query(value = "select b.* from bloc b " +
            " where b.structure_id = ?1 " +
            " and b.deleted is false", nativeQuery = true)
    List<Bloc> getBlocsByStructure(Long idStructure);

    @Query(value = "select count(*) from bloc " +
            " where arrondissement_id = ?1 " +
            " and bloc.deleted is false ", nativeQuery = true)
    int countAllBlocByArrondissement(Long idArrondissement);

    @Query(value = "select b.id, b.cote as cote, b.nom, a.id as arrondissementid " +
            " from arrondissement a" +
            " inner join bloc b on b.arrondissement_id=a.id " +
            " where b.deleted is false " +
            " group by b.id, b.cote, b.nom, a.id ", nativeQuery = true)
    List<BlocSyncResponse> getAdminBlocResponse();

    @Query(value = "select T.blocId as id, T.cote, T.libelle, T.arrondissementid,sum(case when T.enqueteId is null then 0 else 1 end) as nombreEnquete " +
            "    from (  select distinct b.id as blocId, b.cote as cote, b.nom as libelle, a.id as arrondissementid, e.id as enqueteId " +
            " from arrondissement a" +
            " inner join arrondissements_structures sa on sa.arrondissement_id=a.id " +
            " inner join bloc b on b.arrondissement_id=a.id " +
            " left join (select ee.id, ee.bloc_id, ee.status_enquete, ee.deleted from enquete ee where ee.status_enquete in ('FINALISE','REJETE', 'VALIDE')) e on e.bloc_id=b.id " +
            " where (b.deleted is false and e.deleted is false and a.deleted is false) and b.structure_id = ?1)T" +
            " group by  T.blocId, T.cote, T.libelle, T.arrondissementid ",nativeQuery = true)
    List<BlocEnqResponse> getBlocEnqResponse(Long structure_id);

    @Query(value = "select T.blocId as id, T.cote, T.libelle, T.arrondissementid, sum(case when T.enqueteId is null then 0 else 1 end) as nombreEnquete " +
            " from (  select distinct b.id as blocId, b.cote as cote, b.nom as libelle, a.id as arrondissementid, e.id as enqueteId " +
            " from arrondissement a" +
            //" left join arrondissements_structures sa on sa.arrondissement_id=a.id " +
            " inner join bloc b on b.arrondissement_id=a.id " +
            " left join (select ee.id, ee.bloc_id, ee.status_enquete from enquete ee where ee.status_enquete in ('FINALISE','REJETE', 'VALIDE')) e on e.bloc_id=b.id " +
            " where b.deleted is false ) T" +
            " group by  T.blocId, T.cote, T.libelle, T.arrondissementid ",nativeQuery = true)
    List<BlocEnqResponse> getAdminBlocEnqResponse();


    @Query(value = " select " +
            " d.nom as departement, " +
            "c.nom as  commune, " +
            " a.nom as arrondissement, " +
            " q.nom as quartier " +
            " from  departement d " +
            " inner join commune c on c.departement_id=d.id " +
            " inner join arrondissement a on a.commune_id=a.id " +
            " inner join quartier q on q.arrondissement_id=q.id " +
            " inner join blocs_quartiers bq on bq.quartier_id=q.id " +
            " inner join bloc b on b.id=bq.bloc_id " +
            " inner join structure s on s.id=b.structure_id" +
            " where e.delete is false and s.id = ?1 ", nativeQuery = true)
    List<ZoneInterventionParStructureResponse> getZoneInterventionParStructureResponse(Long structure_id);

}