package io.gmss.fiscad.paylaods.response.FicheResponse;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.gmss.fiscad.deserializer.LocalDateDeserializer;
import io.gmss.fiscad.entities.decoupage.Arrondissement;
import io.gmss.fiscad.entities.infocad.metier.Parcelle;
import io.gmss.fiscad.entities.infocad.parametre.Bloc;
import io.gmss.fiscad.entities.infocad.parametre.NatureDomaine;
import io.gmss.fiscad.entities.rfu.metier.CaracteristiqueParcelle;
import io.gmss.fiscad.entities.rfu.metier.EnqueteBatiment;
import io.gmss.fiscad.entities.rfu.metier.EnqueteUniteLogement;
import io.gmss.fiscad.entities.rfu.parametre.Campagne;
import io.gmss.fiscad.entities.rfu.parametre.Equipe;
import io.gmss.fiscad.entities.user.User;
import io.gmss.fiscad.enums.StatusEnquete;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Transient;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDate;
import java.util.List;

@Data
@Getter
@Setter
public class FicheEnquetesResponse {
    private Long id;
    @JsonFormat(pattern = "dd-MM-yyyy")
    @JsonDeserialize(using = LocalDateDeserializer.class)
    private LocalDate dateEnquete;
    private boolean litige;
    private Parcelle parcelle;
    private Campagne campagne;
    private Equipe equipe;
    @Enumerated(EnumType.STRING)
    private StatusEnquete statusEnquete;
    @JsonFormat(pattern = "dd-MM-yyyy")
    @JsonDeserialize(using = LocalDateDeserializer.class)
    private LocalDate dateValidation;
    @JsonFormat(pattern = "dd-MM-yyyy")
    @JsonDeserialize(using = LocalDateDeserializer.class)
    private LocalDate dateRejet;
    private String descriptionMotifRejet;
    @JsonFormat(pattern = "dd-MM-yyyy")
    @JsonDeserialize(using = LocalDateDeserializer.class)
    private LocalDate dateFinalisation;
    @JsonFormat(pattern = "dd-MM-yyyy")
    @JsonDeserialize(using = LocalDateDeserializer.class)
    private LocalDate dateLitigeResolu;
    @JsonIgnore
    private Bloc bloc;
    @JsonFormat(pattern = "dd-MM-yyyy")
    @JsonDeserialize(using = LocalDateDeserializer.class)
    private LocalDate dateSynchronisation;
    private boolean synchronise;
    private String observationParticuliere;
    @Transient
    private String nomPrenomEnqueteur;
    @Transient
    private String nomBloc;
    @Transient
    private String departement;
    @Transient
    private String commune;
    @Transient
    private String arrondissement;
    @Transient
    private String codeBloc;
    @Transient
    private String telEnqueteur;
    @Transient
    private String typeDomaine;

    @JsonIgnore
    private User user;

    @Transient
    private String structureEnqueteur;
//    @JsonIgnore
//    @ManyToOne
//    private Tpe terminal;
//    @Transient
//    private Long terminalId;

    ////////////////////////////////
    private Long quartierId;
    private Long arrondissementId;
    private Long communeId;
    private Long departementId;
    private Long numeroProvisoir;
    private String codeParcelle;
    private String nomProprietaireParcelle;
    private String codeEquipe;
    private String numeroTitreFoncier;

    /// Nouveau champs ajoutés pour RFU Abomey
    private String numEnterParcelle;
    private String numRue;
    private String nomRue;
    private String emplacement;
    private float altitude;
    private float precision;
    private int nbreCoProprietaire;
    private int nbreIndivisiaire;
    private String autreAdresse;
    private String surface;
    private int nbreBatiment;
    private int nbrePiscine;
    @JsonFormat(pattern = "dd-MM-yyyy")
    @JsonDeserialize(using = LocalDateDeserializer.class)
    private LocalDate dateDebutExcemption;
    @JsonFormat(pattern = "dd-MM-yyyy")
    @JsonDeserialize(using = LocalDateDeserializer.class)
    private LocalDate dateFinExcemption;

    private List<CaracteristiqueParcelle> caracteristiquesParcelles;
    private List<EnqueteBatiment> enquetesBatiments;
    private List<EnqueteUniteLogement> enquetesUniteLogements;

    public String getNomPrenomEnqueteur() {
        if (this.user != null) {
            return this.user.getNom() + " " + this.user.getPrenom();
        } else return "";
    }

    public String getNomBloc() {
        if (this.bloc != null) {
            return this.bloc.getNom();
        } else return "";
    }

    public String getCodeBloc() {
        if (this.bloc != null) {
            return this.bloc.getCote();
        } else return "";
    }

    public String getDepartement() {
        if (this.bloc != null) {
            Arrondissement arrondissement = this.bloc.getArrondissement();
            if (arrondissement != null) {
                return arrondissement.getCommune().getDepartement().getNom();
            } else return "";
        } else return "";
    }

    public String getCommune() {
        if (this.bloc != null) {
            Arrondissement arrondissement = this.bloc.getArrondissement();
            if (arrondissement != null) {
                return arrondissement.getCommune().getNom();
            } else return "";
        } else return "";
    }

    public String getArrondissement() {
        if (this.bloc != null) {
            Arrondissement arrondissement = this.bloc.getArrondissement();
            if (arrondissement != null) {
                return arrondissement.getNom();
            } else return "";
        } else return "";
    }

    public String getTypeDomaine() {
        if (this.parcelle != null) {
            NatureDomaine natureDomaine = this.parcelle.getNatureDomaine();
            if (natureDomaine != null) {
                return natureDomaine.getTypeDomaine().getLibelle();
            } else return "";
        } else return "";
    }

    public String getTelEnqueteur() {
        if (this.user != null) {
            return this.user.getTel();
        } else return "";
    }

    public String getStructureEnqueteur() {
        if (this.user != null) {
            return this.user.getStructure().getNom();
        } else return "";
    }
}
