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

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

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.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;

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

import com.iqser.red.service.persistence.management.v1.processor.entity.dossier.DossierEntity;
import com.iqser.red.service.persistence.management.v1.processor.entity.dossier.FileEntity;
import com.iqser.red.service.persistence.management.v1.processor.entity.dossier.ReportTemplateEntity;
import com.iqser.red.service.persistence.management.v1.processor.utils.JSONDownloadFileTypeConverter;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.DownloadFileType;
import com.iqser.red.service.persistence.service.v1.api.shared.model.download.DownloadStatusValue;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldDefaults;

@Data
@Entity
@Builder
@AllArgsConstructor
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Table(name = "download_status")
public class DownloadStatusEntity {

    @Id
    String storageId;
    @Column
    String userId;
    @Column
    String filename;
    @Column
    String mimeType;
    @Column
    @Enumerated(EnumType.STRING)
    DownloadStatusValue status;
    @Column
    OffsetDateTime creationDate;
    @Column
    OffsetDateTime lastDownload;
    @Column
    long fileSize;

    @ManyToOne(fetch = FetchType.EAGER)
    DossierEntity dossier;

    @ManyToMany(fetch = FetchType.EAGER)
    @Fetch(FetchMode.SUBSELECT)
    List<FileEntity> files = new ArrayList<>();

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

    @ManyToMany(fetch = FetchType.LAZY)
    @Fetch(FetchMode.SUBSELECT)
    List<ReportTemplateEntity> reports = new ArrayList<>();

    String redactionPreviewColor;

}
