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

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.gmss.fiscad.deserializer.LocalDateDeserializer;
import io.gmss.fiscad.entities.BaseEntity;
import io.gmss.fiscad.entities.decoupage.Arrondissement;
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.rfu.parametre.ZoneRfu;
import io.gmss.fiscad.entities.user.User;
import io.gmss.fiscad.enums.StatusEnquete;
import io.gmss.fiscad.enums.StatutEnregistrement;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

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

@EqualsAndHashCode(callSuper = true)
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
//@SQLDelete(sql =
//        "UPDATE enquete " +
//                "SET deleted = true " +
//                "WHERE id = ?")
//@Where(clause = " deleted = false")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "origine_enquete", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("INFOCAD")
public class Enquete extends BaseEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @JsonFormat(pattern = "dd-MM-yyyy")
    @JsonDeserialize(using = LocalDateDeserializer.class)
    private LocalDate dateEnquete;
    private boolean litige;

    @JsonIgnore
    @ManyToOne
    private User user;
    private Long mobileDataId;
    @JsonIgnore
    @OneToMany(mappedBy = "enquete")
    private List<ActeurConcerne> acteurConcernes;


    @JsonIgnore
    @ManyToOne
    private Parcelle parcelle;
    private Long parcelleExternalKey;
    @JsonIgnore
    @ManyToOne
    private Bloc bloc;
    @ManyToOne
    private Campagne campagne;

    @JsonIgnore
    @ManyToOne
    private Equipe equipe;

    @JsonIgnore
    @ManyToOne
    private ZoneRfu zoneRfu;
    private String autreNumeroTitreFoncier;
    private Long personneId;
    @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;
    @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;

    @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;

    @JsonIgnore
    @OneToMany(mappedBy = "enquete")
    private List<EnqueteUniteLogement> enqueteUniteLogements;

    @JsonIgnore
    @OneToMany(mappedBy = "enquete")
    private List<EnqueteBatiment> enqueteBatiments;

    @JsonIgnore
    @OneToMany(mappedBy = "enquete")
    private List<CaracteristiqueParcelle> caracteristiqueParcelles;

    @Enumerated(EnumType.STRING)
    private StatutEnregistrement statutEnregistrement;


    public Long getTerminalId() {
        if (this.terminal != null) {
            return this.terminal.getId();
        } else return null;
    }


    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 "";
    }


}
