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

import io.gmss.fiscad.entities.infocad.parametre.Personne;
import io.gmss.fiscad.entities.rfu.metier.DonneesImpositionTfu;
import io.gmss.fiscad.paylaods.response.DonneesImpositionTfuResponse;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;


public interface DonneesImpositionTfuRepository extends JpaRepository<DonneesImpositionTfu, Long> {

    @Query(value = """

            select distinct
            extract(year from e.date_enquete) as annee,
            d.code as codeDepartement,
            d.nom as nomDepartement,
            c.code as codeCommune,
            c.nom as nomCommune,
            a.code as codeArrondissement,
            a.nom as nomArrondissement,
            q.code as codeQuartierVillage,
            q.nom as nomQuartierVillage,
            p.q,
            p.i  as ilot,
            p.p  as parcelle,
            p.nup ,
            p.nup_provisoire as numProvisoire,
            p.latitude,
            p.longitude,
            p.superficie as superficieParc,
            case
                when b.nub is null then 'OUI'
                else 'NON'
                end as batieOuiNon,
            case
                when (e.date_debut_excemption is not null and e.date_debut_excemption<current_date and (e.date_fin_excemption is null or e.date_fin_excemption>current_date)) then 'OUI'
                else 'NON'
                end as exhonereOuiNon,
            case
                when (eb.date_debut_excemption is not null and eb.date_debut_excemption<current_date and (eb.date_fin_excemption is null or eb.date_fin_excemption>current_date)) then 'OUI'
                else 'NON'
                end as batimentExhonereOuiNon,
            case
                when (eul.date_debut_excemption is not null and eul.date_debut_excemption<current_date and (eul.date_fin_excemption is null or eul.date_fin_excemption>current_date)) then 'OUI'
                else 'NON'
                end as uniteLogementExhonereOuiNon,
            case
                when eul.id is not null then eul.montant_mensuel_loyer*12
                when eul.id is null and eb.id is not null then eb.valeur_mensuelle_location*12
                end as montantLoyerAnnuel,
            e.numero_titre_foncier as titreFoncier,
            b.nub as numBatiment,
            ul.nul as numUniteLogement,
            prop.ifu,
            prop.npi,
            prop.tel1 as telProp,
            '' as emailProp,
            prop.nom_ou_sigle as nomProp,
            prop.prenom_ou_raison_sociale as prenomProp,
            prop.prenom_ou_raison_sociale as raisonSociale,
            prop.adresse as adresseProp,
            sc.tel1 as telSc,
            '' as emailSc,
            sc.nom_ou_sigle as nomSc,
            sc.prenom_ou_raison_sociale as prenomSc,
            sc.adresse as adresseSc,
            eb.surface_au_sol as superficieAuSolBat,
            eb.nbre_unite_location as nombreUlog,
            e.nbre_batiment as nombreBat,
            e.nbre_piscine as nombrePiscine,
            e.id as enquete_id,
            e.zone_rfu_id as zoneRfuId,
            eul.surface  as superficieAuSolUlog,
            ba.nom as categorieBat,
            ba.libelle as standingBat,
            ba.tfu_metre_carre as tfuMetreCarre,
            ba.tfu_minimum as tfuMinimum,
            ba.valeur_locative as valeurLocativeAdm,
            b2.structure_id as structureId
        from parcelle p
                 left join quartier q on q.id =p.quartier_id
                 left join arrondissement a on a.id =q.arrondissement_id
                 left join commune c on c.id =a.commune_id
                 left join departement d on d.id=c.departement_id
                 left join (SELECT *
                            FROM enquete e1
                            WHERE date_enquete = (SELECT MAX(e2.date_enquete)
                                                  FROM enquete e2
                                                  WHERE e2.parcelle_id = e1.parcelle_id)
        )e on e.parcelle_id =p.id
                 left join batiment b on b.parcelle_id =p.id
                 left join unite_logement ul on ul.batiment_id =b.id
                 left join (select *
                            from acteur_concerne ac
                                     inner join personne pers on pers.id= ac.personne_id
                            where ac.role_acteur in ('PROPRIETAIRE')
        )prop on prop.enquete_id=e.id
                 left join (select *
                            from acteur_concerne ac
                                     inner join personne pers on pers.id= ac.personne_id
                            where ac.role_acteur in ('PERSONNE_CONTACT')
        )sc on sc.enquete_id=e.id
                 left join enquete_batiment eb on eb.enquete_id = e.id
                 left join enquete_unite_logement eul on eul.enquete_id =e.id
                 left join (select distinct nom,libelle,tfu_metre_carre,tfu_minimum,
            					valeur_locative,enquete_batiment_id,br.arrondissement_id
                            from barem_rfu_bati br
                                     inner join categorie_batiment cb on cb.id =br.categorie_batiment_id
                                     inner join caracteristique c2 on c2.categorie_batiment_id =cb.id
                                     inner join caracteristique_batiment cb2 on cb2.caracteristique_id =c2.id
                                     where c2.categorie_batiment_id  is not null                      \s
        )ba on ba.enquete_batiment_id=eb.id and ba.arrondissement_id=a.id
                 inner join bloc b2 on b2.id =e.bloc_id
        WHERE c.id = ?1 AND extract(year from e.date_enquete) = ?2 
        """, nativeQuery = true)
    List<DonneesImpositionTfuResponse> findAllDonneesImpositionTfu(Long communeId, int annee);

    List<DonneesImpositionTfu> findAllByImpositionsTfu_Id(Long impositionId);
    List<DonneesImpositionTfu> findAllByImpositionsTfu_IdAndCodeArrondissement(Long impositionId,String arrondissementCode);



}
