using IamService.Domain.SeedWork;

namespace IamService.Domain.AggregatesModel.AccessRequestAggregate;

/// <summary>
/// EN: Approver for an access request.
/// VI: Người phê duyệt cho yêu cầu truy cập.
/// </summary>
public class AccessRequestApprover : Entity
{
    private Guid _requestId;
    private Guid _userId;
    private int _order;
    private ApproverStatus _status;
    private DateTime? _respondedAt;
    private string? _comments;

    /// <summary>
    /// EN: Access request ID.
    /// VI: ID yêu cầu truy cập.
    /// </summary>
    public Guid RequestId => _requestId;

    /// <summary>
    /// EN: Approver user ID.
    /// VI: ID user phê duyệt.
    /// </summary>
    public Guid UserId => _userId;

    /// <summary>
    /// EN: Order in approval chain (1 = first).
    /// VI: Thứ tự trong chuỗi phê duyệt (1 = đầu tiên).
    /// </summary>
    public int Order => _order;

    /// <summary>
    /// EN: Approver status.
    /// VI: Trạng thái phê duyệt.
    /// </summary>
    public ApproverStatus Status => _status;

    /// <summary>
    /// EN: When approver responded.
    /// VI: Thời gian phản hồi.
    /// </summary>
    public DateTime? RespondedAt => _respondedAt;

    /// <summary>
    /// EN: Approver comments.
    /// VI: Nhận xét của người phê duyệt.
    /// </summary>
    public string? Comments => _comments;

    protected AccessRequestApprover() 
    { 
        _status = ApproverStatus.Pending;
    }

    public AccessRequestApprover(Guid requestId, Guid userId, int order)
    {
        Id = Guid.NewGuid();
        _requestId = requestId;
        _userId = userId;
        _order = order;
        _status = ApproverStatus.Pending;
    }

    public void Approve(string? comments = null)
    {
        _status = ApproverStatus.Approved;
        _respondedAt = DateTime.UtcNow;
        _comments = comments;
    }

    public void Reject(string? comments = null)
    {
        _status = ApproverStatus.Rejected;
        _respondedAt = DateTime.UtcNow;
        _comments = comments;
    }
}

/// <summary>
/// EN: Approver status enumeration.
/// VI: Enumeration trạng thái phê duyệt.
/// </summary>
public class ApproverStatus : Enumeration
{
    public static readonly ApproverStatus Pending = new(1, nameof(Pending));
    public static readonly ApproverStatus Approved = new(2, nameof(Approved));
    public static readonly ApproverStatus Rejected = new(3, nameof(Rejected));

    public ApproverStatus(int id, string name) : base(id, name) { }
}
