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

import io.gmss.fiscad.entities.infocad.metier.Enquete;
import io.gmss.fiscad.entities.infocad.parametre.Personne;
import io.gmss.fiscad.paylaods.response.*;
import io.gmss.fiscad.paylaods.response.report.EnqueteParBlocResponse;
import io.gmss.fiscad.paylaods.response.restoration.EnquetePayLoad;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import javax.swing.text.html.Option;
import java.util.List;
import java.util.Optional;

public interface EnqueteRepository extends JpaRepository<Enquete, Long> {
     Optional<Enquete> findFirstByCodeParcelle(String codeParcelle);
    Optional<Enquete> findByMobileDataId(Long id);
    @Query(value = "select distinct " +
            " e.id," +
            " e.parcelle_id as parcelleId," +
            " c.id as communeId, " +
            " c.nom as communeNom, " +
            " a.id as arrondissementId, " +
            " a.nom as arrondissementNom, " +
            " b.id as blocId," +
            " b.nom as blocNom, " +
            " s.id as structureId, " +
            " s.nom as structureNom, " +
            " e.litige," +
            " e.status_enquete as statusEnquete, " +
            " e.date_finalisation as dateFinalisation, " +
            " e.date_validation as dateValidation, " +
            " e.date_synchronisation as dateSynchronisation, " +
            " e.synchronise, " +
            " e.terminal_id as terminalId, " +
            " per.nom_ou_sigle as nomSigle, " +
            " per.prenom_ou_raison_sociale as prenomRaisonSociale," +
            " tp.libelle as typePersonne," +
            " td.libelle as typeDomaine, " +
            " nd.libelle as natureDomaine, " +
            " p.nup_provisoire as nupProvisoire " +
            " from enquete e " +
            " left join bloc b on b.id=e.bloc_id " +
            " left join arrondissement a on a.id= b.arrondissement_id  " +
            " left join commune c on c.id=a.commune_id " +
            " inner join structure s on s.id=b.structure_id " +
            " inner join parcelle p on p.id=e.parcelle_id " +
            " left join nature_domaine nd on nd.id=p.nature_domaine_id " +
            " left join type_domaine td on td.id=nd.type_domaine_id " +
            " inner join acteur_concerne ac on ac.enquete_id=e.id " +
            " inner join personne per on per.id=ac.personne_id " +
            " left join type_personne tp on tp.id=per.type_personne_id " +
            " where e.deleted is false and ac.role_acteur='PROPRIETAIRE'"
            , nativeQuery = true)
    List<EnqueteResponse> getEnqueteComArrondBloc();


    Optional<Enquete> findFirstByExternalKey(Long externalKey);

    @Query(value = "select distinct " +
            " e.id as idBackend," +
            " e.external_key as externalKey, " +
            " e.status_enquete as statusEnquete, " +
            " e.description_motif_rejet as motif, " +
            " e.terminal_id as terminalId " +
            " from enquete e " +
            " where e.deleted is false and e.synchronise is false " +
            " and e.terminal_id= ?1"
            , nativeQuery = true)
    List<EnqueteNonSyncResponse> getEnqueteValNonSync(Long terminalId);

    @Query(value = "select " +
            " e.status_enquete as  statutEnquete," +
            " count(*) as nombre " +
            " from enquete e " +
            " where e.deleted is false " +
            " group by e.status_enquete"
            , nativeQuery = true)
    List<StatEnqueteParStatutResponse> getStatEnqueteParStatut();

    @Query(value = "select " +
            " e.status_enquete as  statutEnquete, " +
            " count(*) as nombre " +
            " from enquete e " +
            " inner join bloc b on b.id=e.bloc_id " +
            " inner join structure s on s.id = b.structure_id " +
            " inner join users u on u.structure_id=s.id " +
            " where e.deleted is false and u.id = ?1 " +
            " group by e.status_enquete "
            , nativeQuery = true)
    List<StatEnqueteParStatutResponse> getStatEnqueteParStatut(Long userId);

    @Query(value = "select " +
            " b.cote as  bloc, " +
            " SUM(case when e.status_enquete='VALIDE' then 1 else 0 end) + " +
            " SUM(case when e.status_enquete='REJETE' then 1 else 0 end) + " +
            " SUM(case when e.status_enquete='FINALISE' then 1 else 0 end)  as total, " +
            " SUM(case when e.status_enquete='VALIDE' then 1 else 0 end) as valide, " +
            " SUM(case when e.status_enquete='REJETE' then 1 else 0 end) as rejet, " +
            " SUM(case when e.status_enquete='FINALISE' then 1 else 0 end) as finalise " +
            " from bloc b " +
            " left join enquete e on b.id = e.bloc_id and e.deleted = false" +
            " left join structure s on s.id = b.structure_id and s.deleted = false" +
            " left join users u on u.structure_id = s.id  and u.deleted = false" +
            " where b.deleted = false and u.id = ?1 " +
            " group by b.cote "
            ,nativeQuery = true)
    List<StatEnqueteParBlocResponse> getStatEnqueteParBloc(Long idUser);

    @Query(value = "select " +
            " a.nom as  arrondissement, " +
            " SUM(case when e.status_enquete='VALIDE' then 1 else 0 end) + " +
            " SUM(case when e.status_enquete='REJETE' then 1 else 0 end) + " +
            " SUM(case when e.status_enquete='FINALISE' then 1 else 0 end)  as total, " +
            " SUM(case when e.status_enquete='VALIDE' then 1 else 0 end) as valide, " +
            " SUM(case when e.status_enquete='REJETE' then 1 else 0 end) as rejet, " +
            " SUM(case when e.status_enquete='FINALISE' then 1 else 0 end) as finalise " +
            " from arrondissement a" +
            " left join bloc b on a.id = b.arrondissement_id and b.deleted = false " +
            " left join enquete e on b.id = e.bloc_id and e.deleted = false " +
            " where a.deleted = false and a.commune_id = ?1 " +
            " group by a.nom "
            ,nativeQuery = true)
    List<StatEnqueteAdminDecoupageResponse> getStatEnqueteParDecoupage(Long idCommune);


    @Query(value = "select " +
            " s.nom as  structure, " +
            " SUM(case when e.status_enquete='VALIDE' then 1 else 0 end) + " +
            " SUM(case when e.status_enquete='REJETE' then 1 else 0 end) + " +
            " SUM(case when e.status_enquete='FINALISE' then 1 else 0 end)  as total, " +
            " SUM(case when e.status_enquete='VALIDE' then 1 else 0 end) as valide, " +
            " SUM(case when e.status_enquete='REJETE' then 1 else 0 end) as rejet, " +
            " SUM(case when e.status_enquete='FINALISE' then 1 else 0 end) as finalise " +
            " from structure s " +
            " left join bloc b on b.structure_id = s.id and b.deleted = false " +
            " left join enquete e on e.bloc_id = b.id and e.deleted = false " +
            " where s.deleted = false " +
            " group by s.nom "
            ,nativeQuery = true)
    List<StatEnqueteAdminStructureResponse> getStatEnqueteParStructure();

    Optional<Enquete> findFirstByExternalKeyAndTerminal_Id(Long externalKey, Long terminalId);

    List<Enquete> findAllByUser_Id(Long userId);


    @Query(value = "SELECT " +
            " e.id as idBackend," +
            " e.external_key as externalKey," +
            " e.date_enquete as dateEnquete," +
            " e.date_finalisation as dateFinalisation, " +
            " e.litige," +
            " e.user_id as userId," +
            " p.external_key as parcelleId," +
            " e.bloc_id as blocId, " +
            " e.status_enquete as statusEnquete, " +
            " e.description_motif_rejet as descriptionMotifRejet," +
            " e.observation_particuliere as observation," +
            " e.synchronise," +
            " e.terminal_id as terminalId, " +

            " e.quartier_id as quartierId, " +
            " e.arrondissement_id as arrondissementId, " +
            " e.commune_id as communeId, " +
            " e.terminal_id as departementId, " +
            " e.departement_id as numeroProvisoir, " +
            " e.code_parcelle as codeParcelle, " +
            " e.nom_proprietaire_parcelle as nomProprietaireParcelle, " +
            " e.code_equipe as codeEquipe, " +
            " e.numero_titre_foncier as numeroTitreFoncier " +

            " e.num_enter_parcelle as numEnterParcelle" +
            " e.num_rue as numRue" +
            " e.num_nom_rue as nomRue" +
            " e.emplacement as emplacement" +
            " e.altitude as altitude" +
            " e.precision as precision" +
            " e.nbre_co_proprietaire as nbreCoProprietaire" +
            " e.nbre_indivisiaire as nbreIndivisiaire" +
            " e.autre_adresse as autreAdresse" +
            " e.surface as surface" +
            " e.nbre_batiment as nbreBatiment" +
            " e.nbre_piscine as nbrePiscine" +
            " e.date_debut_excemption as dateDebutExcemption" +
            " e.date_fin_excemption as dateFinExcemption" +
            " FROM enquete e inner join parcelle p on p.id = e.parcelle_id " +
            " WHERE e.terminal_id = ?1", nativeQuery = true)
    List<EnquetePayLoad> getEnqueteByTerminalId(Long terminalId);


    @Query(value = "SELECT " +
            " p.nup_provisoire as nupProvisoire," +
            " q.nom as quartier, " +
            " p.q," +
            " p.i," +
            " p.p, " +
            " e.litige," +
            " n.libelle as nature," +
            " coalesce(pers.nom_ou_sigle,'') || ' ' || coalesce(pers.prenom_ou_raison_sociale,'') as nomProprietaire," +
            " pers.tel1 as telephone " +
            " FROM enquete e " +
            " inner join parcelle p on p.id = e.parcelle_id" +
            " inner join acteur_concerne ac on ac.enquete_id=e.id " +
            " inner join personne pers on pers.id=ac.personne_id " +
            " inner join nature_domaine n on n.id=p.nature_domaine_id" +
            " inner join quartier q on q.id=e.quartier_id" +
            " WHERE p.deleted is false and e.bloc_id = ?1 and ac.role_acteur=?2 ", nativeQuery = true)
    List<EnqueteParBlocResponse> getEnqueteByBloc(Long BlocId, String roleActeur);


    Optional<Enquete> findFirstByParcelle_NupProvisoireOrderByDateEnqueteDesc(String numProvisoire);


    @Query(value = "SELECT id, " +
            " code_parcelle as codeParcelle," +
            " nbre_batiment as nombreBatiment " +
            " FROM enquete e " +
            " WHERE e.deleted is false and e.code_parcelle = ?1 ", nativeQuery = true)
    List<EnqueteCheckResponse> getEnquetByNupProvisoir(String numProvisoire);


    Optional<Enquete> findFirstByParcelle_IdOrderByDateEnqueteDescIdDesc(Long parcelleId);


}
