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

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.gmss.fiscad.entities.BaseEntity;
import io.gmss.fiscad.entities.decoupage.Commune;
import io.gmss.fiscad.entities.decoupage.Nationalite;
import io.gmss.fiscad.entities.infocad.metier.ActeurConcerne;
import io.gmss.fiscad.entities.infocad.metier.Piece;
import io.gmss.fiscad.entities.infocad.metier.Tpe;
import io.gmss.fiscad.entities.infocad.metier.Upload;
import io.gmss.fiscad.entities.metadata.MobileDataPersonne;
import io.gmss.fiscad.enums.Categorie;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

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

@EqualsAndHashCode(callSuper = true)
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@SQLDelete(sql =
        "UPDATE personne " +
                "SET deleted = true " +
                "WHERE id = ?")
@Where(clause = " deleted = false")
public class Personne extends BaseEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String ifu;
    private String nomOuSigle;
    private String prenomOuRaisonSociale;
    private String numRavip;
    private String npi;
    private String dateNaissanceOuConsti;
    private String lieuNaissance;
    private String tel1;
    private String tel2;
    private String adresse;
    @Enumerated(EnumType.STRING)
    private Categorie categorie;
    @ManyToOne
    private SituationMatrimoniale situationMatrimoniale;
    @ManyToOne
    private Nationalite nationalite;
    @ManyToOne
    private TypePersonne typePersonne;
    @ManyToOne
    private Profession profession;
    @ManyToOne
    private Commune commune;
    @JsonIgnore
    @ManyToOne
    private Tpe terminal;
    @ColumnDefault("0")
    private int haveRepresentant;
    private String ravipQuestion;
    private String age;
    private String nomJeuneFille;
    private String nomMere;
    private String prenomMere;
    private String indicatifTel1;
    private String indicatifTel2;
    private String sexe;
    @ColumnDefault("0")
    private int mustHaveRepresentant;
    private String filePath;
    private String observation;
    @ColumnDefault("false")
    private boolean synchronise;

    @OneToMany(mappedBy = "personne")
    private List<Upload> uploads;

    @OneToMany(mappedBy = "personne")
    private List<Piece> pieces;

    private Long mobileDataId;
    public List<Upload> getUploads() {
        return uploads.stream().filter(upload -> upload.getPiece() == null && upload.getMembreGroupe() == null).toList();
    }

    public List<Piece> getPieces() {
        return pieces.stream().filter(piece -> piece.getActeurConcerne() == null).toList();
    }
}
