package net.siegeln.cameleer.saas.certificate;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.PrePersist;
import jakarta.persistence.Table;

import java.time.Instant;
import java.util.UUID;

@Entity
@Table(name = "certificates")
public class CertificateEntity {

    public enum Status { ACTIVE, STAGED, ARCHIVED }

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID id;

    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 10)
    private Status status;

    @Column(name = "subject", length = 500)
    private String subject;

    @Column(name = "issuer", length = 500)
    private String issuer;

    @Column(name = "not_before")
    private Instant notBefore;

    @Column(name = "not_after")
    private Instant notAfter;

    @Column(name = "fingerprint", length = 128)
    private String fingerprint;

    @Column(name = "has_ca", nullable = false)
    private boolean hasCa;

    @Column(name = "self_signed", nullable = false)
    private boolean selfSigned;

    @Column(name = "uploaded_by")
    private UUID uploadedBy;

    @Column(name = "created_at", nullable = false, updatable = false)
    private Instant createdAt;

    @Column(name = "activated_at")
    private Instant activatedAt;

    @Column(name = "archived_at")
    private Instant archivedAt;

    @PrePersist
    protected void onCreate() {
        if (createdAt == null) createdAt = Instant.now();
    }

    // --- Getters and setters ---

    public UUID getId() { return id; }

    public Status getStatus() { return status; }
    public void setStatus(Status status) { this.status = status; }

    public String getSubject() { return subject; }
    public void setSubject(String subject) { this.subject = subject; }

    public String getIssuer() { return issuer; }
    public void setIssuer(String issuer) { this.issuer = issuer; }

    public Instant getNotBefore() { return notBefore; }
    public void setNotBefore(Instant notBefore) { this.notBefore = notBefore; }

    public Instant getNotAfter() { return notAfter; }
    public void setNotAfter(Instant notAfter) { this.notAfter = notAfter; }

    public String getFingerprint() { return fingerprint; }
    public void setFingerprint(String fingerprint) { this.fingerprint = fingerprint; }

    public boolean isHasCa() { return hasCa; }
    public void setHasCa(boolean hasCa) { this.hasCa = hasCa; }

    public boolean isSelfSigned() { return selfSigned; }
    public void setSelfSigned(boolean selfSigned) { this.selfSigned = selfSigned; }

    public UUID getUploadedBy() { return uploadedBy; }
    public void setUploadedBy(UUID uploadedBy) { this.uploadedBy = uploadedBy; }

    public Instant getCreatedAt() { return createdAt; }

    public Instant getActivatedAt() { return activatedAt; }
    public void setActivatedAt(Instant activatedAt) { this.activatedAt = activatedAt; }

    public Instant getArchivedAt() { return archivedAt; }
    public void setArchivedAt(Instant archivedAt) { this.archivedAt = archivedAt; }

    public static CertificateEntity fromInfo(CertificateInfo info, Status status) {
        var entity = new CertificateEntity();
        entity.setStatus(status);
        entity.setSubject(info.subject());
        entity.setIssuer(info.issuer());
        entity.setNotBefore(info.notBefore());
        entity.setNotAfter(info.notAfter());
        entity.setFingerprint(info.fingerprint());
        entity.setHasCa(info.hasCaBundle());
        entity.setSelfSigned(info.selfSigned());
        return entity;
    }
}
