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

namespace SolutionErp.Domain.PurchaseEvaluations;

// Versioned workflow definition cho module Duyệt NCC — pattern giống HĐ.
// Invariant: AT MOST ONE IsActive=true per PurchaseEvaluationType.
// PurchaseEvaluation.WorkflowDefinitionId pin tại create.
public class PurchaseEvaluationWorkflowDefinition : BaseEntity
{
    public string Code { get; set; } = string.Empty;
    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();
}

// Workflow Step PE (Session 16 — Mig 21 drastic refactor):
// Mỗi step = 1 (Phòng × Cấp + Approvers users). Sequential per Order.
// Service iterate steps OrderBy(Order), advance PE.CurrentWorkflowStepIndex.
public class PurchaseEvaluationWorkflowStep : BaseEntity
{
    public Guid PurchaseEvaluationWorkflowDefinitionId { get; set; }
    public int Order { get; set; }
    public PurchaseEvaluationPhase Phase { get; set; }          // [DEPRECATED] dùng ChoDuyet=10 cho new
    public string Name { get; set; } = string.Empty;            // "Phòng A — Cấp 1"
    public int? SlaDays { get; set; }

    // Mig 21 — Phòng × Cấp. Approver match Dept + PositionLevel (OR cùng cấp+phòng).
    public Guid? DepartmentId { get; set; }
    public PositionLevel? PositionLevel { 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; }
}
