using SolutionErp.Domain.Common;

namespace SolutionErp.Domain.Contracts;

// 2-stage department approval cho HĐ workflow (Phase 9 — Migration 16).
// Mỗi phase × phòng ban có max 2 row: Stage=Review (NV duyệt) + Stage=Confirm
// (TPB duyệt). Workflow service guard: chỉ transition khi đã có Stage=Confirm.
//
// User.CanBypassReview=true → NV insert thẳng Stage=Confirm (IsBypassed=true).
//
// UNIQUE (ContractId, PhaseAtApproval, DepartmentId, Stage) — 1 phase × 1 phòng
// × 1 stage = 1 row duy nhất. UPDATE in-place khi user đổi ý (audit qua
// ContractChangelog).
public class ContractDepartmentApproval : AuditableEntity
{
    public Guid ContractId { get; set; }
    public int PhaseAtApproval { get; set; }              // snapshot ContractPhase int (Phase tại lúc approve)
    public Guid DepartmentId { get; set; }
    public ApprovalStage Stage { get; set; }               // 1=Review (NV), 2=Confirm (TPB)
    public Guid ApproverUserId { get; set; }
    public string? ApproverRoleSnapshot { get; set; }       // VD "NV.CCM" / "TPB.CCM" — denorm cho audit readable
    public string? Comment { get; set; }
    public DateTime ApprovedAt { get; set; }
    public bool IsBypassed { get; set; }                    // true nếu NV bypass (User.CanBypassReview=true)

    public Contract? Contract { get; set; }
}
