using SolutionErp.Domain.Common;

namespace SolutionErp.Domain.Contracts;

// Versioned workflow definition per ContractType. Admin can edit quy trình =>
// creates a NEW version; old version stays for contracts already pinned to it.
//
// Invariant: for any ContractType, AT MOST ONE WorkflowDefinition has
// IsActive=true at any given time. Contract.WorkflowDefinitionId pins the
// contract to its chosen version at creation time — so changing the active
// version does not retroactively affect running contracts.
public class WorkflowDefinition : BaseEntity
{
    public string Code { get; set; } = string.Empty;            // "QT-TP-NCC" admin-editable
    public int Version { get; set; }                            // monotonically increases per Code
    public ContractType ContractType { get; set; }
    public string Name { get; set; } = string.Empty;            // display label
    public string? Description { get; set; }
    public bool IsActive { get; set; }                          // only one per ContractType
    public DateTime? ActivatedAt { get; set; }

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

public class WorkflowStep : BaseEntity
{
    public Guid WorkflowDefinitionId { get; set; }
    public int Order { get; set; }                              // 1-based sequence
    public ContractPhase Phase { get; set; }                    // which ContractPhase this step represents
    public string Name { get; set; } = string.Empty;            // display, can differ from Phase label
    public int? SlaDays { get; set; }                           // null = no SLA for this step

    public WorkflowDefinition? WorkflowDefinition { get; set; }
    public List<WorkflowStepApprover> Approvers { get; set; } = new();
}

public enum WorkflowApproverKind
{
    Role = 1,  // AssignmentValue = role name (AppRoles.*)
    User = 2,  // AssignmentValue = user id (Guid as string)
}

public class WorkflowStepApprover : BaseEntity
{
    public Guid WorkflowStepId { get; set; }
    public WorkflowApproverKind Kind { get; set; }
    public string AssignmentValue { get; set; } = string.Empty;

    public WorkflowStep? Step { get; set; }
}
