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

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
import io.gmss.fiscad.entities.BaseEntity;
import io.gmss.fiscad.entities.infocad.parametre.Personne;
import io.gmss.fiscad.entities.rfu.metier.EnqueteBatiment;
import io.gmss.fiscad.entities.rfu.metier.EnqueteUniteLogement;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.Type;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

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

@Entity
@Getter
@Setter
public class Upload extends BaseEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String Observation;
    private boolean synchronise;
    private String fileName;
    private String originalFileName;
    @Transient
    private String URIFile;
    private String checkSum;
    private long size;
    private String mimeType;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    private Piece piece;
    private Long pieceExternalKey;

    private Long mobileDataId;
    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    private MembreGroupe membreGroupe;
    private Long membreGroupeExternalKey;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    private Tpe terminal;

    private String name;
    private String filePath;
    private Long max_numero_piece_id;
    private Long max_numero_upload_id;
    private Long max_numero_acteur_concerne_id;
    private Long enqueteId;
    private Long blocId;
    private String reference;
    private String description;
    @ColumnDefault("0")
    private int nombreParcelleGeom;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "enquete_batiment_id")
    private EnqueteBatiment enqueteBatiment;
    private Long enqueteBatimentExternalKey;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "enquete_unite_logement_id")
    private EnqueteUniteLogement enqueteUniteLogement;

    private Long enqueteUniteLogementExternalKey;

    @JsonIgnore
    @ManyToOne (fetch = FetchType.LAZY)
    @JoinColumn(name = "personne_id")
    private Personne personne ;
    private Long personneExternalKey ;

    @Type(JsonBinaryType.class)
    @Column(columnDefinition = "jsonb")
    private Upload uploadJson;

    public Upload() {
    }

    public String getURIFile() {
        String url = this.serverContext() + fileName;
        System.out.println("url = " + url);
       // return url != null ? url.toLowerCase().startsWith("https") ? url : url.toLowerCase().replaceFirst("http", "https") : null;
        return url != null ? url.toLowerCase().startsWith("https") ? url : url.toLowerCase().replaceFirst("http", "https") : null;

    }

    private String serverContext() {
        String url= ServletUriComponentsBuilder.fromCurrentContextPath()
                .path("/api/upload/downloadFile/")
                .toUriString();
        if(!url.contains("8282/api")){
            url.replace("/api",":8282/api");
        }

            return url;
    }

    @Override
    public String toString() {
        return "Upload{" +
                "id=" + id +
                ", fileName='" + fileName + '\'' +
                ", originalFileName='" + originalFileName + '\'' +
                '}';

    }
}