using SolutionErp.Domain.Common;
using SolutionErp.Domain.Contracts;   // reuse WorkflowApproverKind

namespace SolutionErp.Domain.PurchaseEvaluations;

// Versioned workflow definition cho module Duyệt NCC — pattern giống HĐ
// nhưng tách table riêng vì Phase là PurchaseEvaluationPhase enum (không
// phải ContractPhase). Admin có UI /system/pe-workflows/:typeCode tương
// tự /system/workflows/:typeCode.
//
// Invariant: AT MOST ONE IsActive=true per PurchaseEvaluationType tại 1
// thời điểm. PurchaseEvaluation.WorkflowDefinitionId pin tại create →
// phiếu cũ không bị ảnh hưởng khi admin active version mới.
public class PurchaseEvaluationWorkflowDefinition : BaseEntity
{
    public string Code { get; set; } = string.Empty;             // "QT-DN-A" / "QT-DN-B" default
    public int Version { get; set; }
    public PurchaseEvaluationType EvaluationType { get; set; }
    public string Name { get; set; } = string.Empty;
    public string? Description { get; set; }
    public bool IsActive { get; set; }
    public DateTime? ActivatedAt { get; set; }

    public List<PurchaseEvaluationWorkflowStep> Steps { get; set; } = new();
}

public class PurchaseEvaluationWorkflowStep : BaseEntity
{
    public Guid PurchaseEvaluationWorkflowDefinitionId { get; set; }
    public int Order { get; set; }
    public PurchaseEvaluationPhase Phase { get; set; }
    public string Name { get; set; } = string.Empty;
    public int? SlaDays { get; set; }

    public PurchaseEvaluationWorkflowDefinition? Definition { get; set; }
    public List<PurchaseEvaluationWorkflowStepApprover> Approvers { get; set; } = new();
}

public class PurchaseEvaluationWorkflowStepApprover : BaseEntity
{
    public Guid PurchaseEvaluationWorkflowStepId { get; set; }
    public WorkflowApproverKind Kind { get; set; }              // reuse Role/User enum từ Contract
    public string AssignmentValue { get; set; } = string.Empty;

    public PurchaseEvaluationWorkflowStep? Step { get; set; }
}
