package gc.mda.kcg.domain.fleet;

import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;

import java.time.OffsetDateTime;
import java.util.Map;
import java.util.UUID;

/**
 * 모선 추론 학습 세션 (운영자가 정답 라벨링).
 */
@Entity
@Table(name = "gear_parent_label_sessions", schema = "kcg")
@Getter @Setter @NoArgsConstructor @AllArgsConstructor @Builder
public class LabelSession {

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

    @Column(name = "group_key", nullable = false, length = 255)
    private String groupKey;

    @Column(name = "sub_cluster_id", nullable = false)
    private Integer subClusterId;

    @Column(name = "label_parent_mmsi", nullable = false, length = 20)
    private String labelParentMmsi;

    @Column(name = "status", nullable = false, length = 20)
    private String status;  // ACTIVE, CANCELLED, COMPLETED

    @Column(name = "active_from", nullable = false)
    private OffsetDateTime activeFrom;

    @Column(name = "active_until")
    private OffsetDateTime activeUntil;

    @JdbcTypeCode(SqlTypes.JSON)
    @Column(name = "anchor_snapshot", columnDefinition = "jsonb")
    private Map<String, Object> anchorSnapshot;

    @JdbcTypeCode(SqlTypes.UUID)
    @Column(name = "created_by")
    private UUID createdBy;

    @Column(name = "created_by_acnt", length = 50)
    private String createdByAcnt;

    @JdbcTypeCode(SqlTypes.UUID)
    @Column(name = "cancelled_by")
    private UUID cancelledBy;

    @Column(name = "cancelled_at")
    private OffsetDateTime cancelledAt;

    @Column(name = "cancel_reason", columnDefinition = "text")
    private String cancelReason;

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

    @PrePersist
    void prePersist() {
        OffsetDateTime now = OffsetDateTime.now();
        if (createdAt == null) createdAt = now;
        if (activeFrom == null) activeFrom = now;
        if (status == null) status = "ACTIVE";
    }
}
