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

import jakarta.persistence.*;
import lombok.*;

import java.math.BigDecimal;

@Entity
@Table(name = "wbs_nodes",
        uniqueConstraints = @UniqueConstraint(columnNames = {"wbs_version_id", "wbs_code"}))
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class WbsNode {

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "wbs_version_id", nullable = false)
    private WbsVersion wbsVersion;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id")
    private WbsNode parent;

    @Column(name = "wbs_code", nullable = false, length = 100)
    private String wbsCode;

    @Column(nullable = false)
    private Integer level;

    @Column(nullable = false, length = 500)
    private String name;

    @Column(length = 50)
    private String discipline;

    @Column(name = "planned_hours", precision = 10, scale = 2)
    private BigDecimal plannedHours;

    @Column(name = "sort_order")
    @Builder.Default
    private Integer sortOrder = 0;

    @Column(name = "is_leaf")
    @Builder.Default
    private Boolean isLeaf = false;

    /**
     * Content equality check for version comparison.
     */
    public boolean contentEquals(WbsNode other) {
        if (other == null) return false;
        return java.util.Objects.equals(this.name, other.name)
                && java.util.Objects.equals(this.level, other.level)
                && java.util.Objects.equals(this.discipline, other.discipline)
                && (this.plannedHours == null ? other.plannedHours == null
                    : this.plannedHours.compareTo(other.plannedHours) == 0);
    }
}
