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

import jakarta.persistence.*;
import kr.co.accura.wbx.spring.common.BaseEntity;
import lombok.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "approvals", uniqueConstraints = {
    @UniqueConstraint(columnNames = {"timesheet_id"})
})
@Getter @Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class TtApproval extends BaseEntity {

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

    @Column(name = "timesheet_id", nullable = false)
    private Long timesheetId;

    @Column(name = "requester_id", nullable = false)
    private Long requesterId;

    @Column(name = "project_id")
    private Long projectId;

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

    @Column(name = "submitted_at")
    private LocalDateTime submittedAt;

    @Column(name = "completed_at")
    private LocalDateTime completedAt;

    @OneToMany(mappedBy = "approval", cascade = CascadeType.ALL, orphanRemoval = true)
    @Builder.Default
    private List<TtApprovalLine> lines = new ArrayList<>();

    @OneToMany(mappedBy = "approval", cascade = CascadeType.ALL, orphanRemoval = true)
    @Builder.Default
    private List<TtApprovalComment> comments = new ArrayList<>();

    public void complete() {
        this.status = "APPROVED";
        this.completedAt = LocalDateTime.now();
    }

    public void reject() {
        this.status = "REJECTED";
        this.completedAt = LocalDateTime.now();
    }
}
