package com.iqser.red.service.persistence.management.v1.processor.entity.dossier;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.iqser.red.service.persistence.management.v1.processor.entity.configuration.TypeEntity;
import com.iqser.red.service.persistence.management.v1.processor.entity.configuration.WatermarkEntity;
import com.iqser.red.service.persistence.management.v1.processor.utils.JSONDownloadFileTypeConverter;
import com.iqser.red.service.persistence.management.v1.processor.utils.JSONStringSetConverter;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.DownloadFileType;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.DossierVisibility;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@Data
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "dossier")
@EqualsAndHashCode(of = {"id"})
public class DossierEntity {

    @Id
    private String id;

    @Column
    private String dossierName;

    @Column
    private OffsetDateTime date;

    @Column
    private OffsetDateTime lastUpdated;

    @Column(length = 4000)
    private String description;

    @Column
    private String ownerId;

    @Builder.Default
    @Column(columnDefinition = "text", name = "members")
    @Convert(converter = JSONStringSetConverter.class)
    private Set<String> memberIds = new HashSet<>();

    @Builder.Default
    @Column(columnDefinition = "text", name = "approvers")
    @Convert(converter = JSONStringSetConverter.class)
    private Set<String> approverIds = new HashSet<>();

    @Builder.Default
    @Column(columnDefinition = "text", name = "download_file_types")
    @Convert(converter = JSONDownloadFileTypeConverter.class)
    private Set<DownloadFileType> downloadFileTypes = new HashSet<>();

    @Column
    private OffsetDateTime softDeletedTime;

    @Column
    private OffsetDateTime hardDeletedTime;

    @Column
    private OffsetDateTime archivedTime;

    @Column
    private OffsetDateTime startDate;

    @Column
    private OffsetDateTime dueDate;

    @Column
    @Enumerated(EnumType.STRING)
    private DossierVisibility visibility;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    private DossierTemplateEntity dossierTemplate;

    @Column(updatable = false, insertable = false, name = "dossier_template_id")
    private String dossierTemplateId;

    @Builder.Default
    @Fetch(FetchMode.SUBSELECT)
    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "dossiers")
    private List<ReportTemplateEntity> reportTemplates = new ArrayList<>();

    @Builder.Default
    @Fetch(FetchMode.SUBSELECT)
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "dossier", fetch = FetchType.LAZY)
    private List<TypeEntity> dossierTypes = new ArrayList<>();

    @Builder.Default
    @Fetch(FetchMode.SUBSELECT)
    @JoinColumn(name = "dossier_id")
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<DossierAttributeEntity> dossierAttributes = new ArrayList<>();

    @ManyToOne(fetch = FetchType.LAZY)
    private DossierStatusEntity dossierStatus;

    @Column(updatable = false, insertable = false, name = "dossier_status_id")
    private String dossierStatusId;

    @Column(updatable = false, insertable = false, name = "watermark_id")
    private Long watermarkId;

    @ManyToOne(fetch = FetchType.LAZY)
    private WatermarkEntity watermark;

    @Column(updatable = false, insertable = false, name = "preview_watermark_id")
    private Long previewWatermarkId;

    @ManyToOne(fetch = FetchType.LAZY)
    private WatermarkEntity previewWatermark;


    public boolean isDeleted() {

        return softDeletedTime != null || hardDeletedTime != null;
    }

}
