package kr.co.accura.wtm.domain.teal.entity;

import jakarta.persistence.*;
import kr.co.accura.wtm.domain.project.entity.Project;
import lombok.*;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Entity
@Table(name = "teal_versions",
        uniqueConstraints = @UniqueConstraint(columnNames = {"project_id", "version_number"}))
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class TealVersion {

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "project_id", nullable = false)
    private Project project;

    @Column(name = "version_number", nullable = false)
    private Integer versionNumber;

    @Column(name = "effective_date", nullable = false)
    private LocalDate effectiveDate;

    @Column(length = 500)
    private String description;

    @Column(length = 20)
    @Builder.Default
    private String status = "DRAFT";

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

    @Column(name = "created_at")
    @Builder.Default
    private LocalDateTime createdAt = LocalDateTime.now();

    public void activate() {
        this.status = "ACTIVE";
    }

    public Long getProjectId() {
        return project != null ? project.getId() : null;
    }

    public static TealVersion create(Project project, LocalDate effectiveDate, int versionNumber) {
        return TealVersion.builder()
                .project(project)
                .versionNumber(versionNumber)
                .effectiveDate(effectiveDate)
                .status("DRAFT")
                .build();
    }
}
