using System.Text.Json;
using WhatsAppService.Domain.SeedWork;

namespace WhatsAppService.Domain.AggregatesModel.AutomationFlowAggregate;

/// <summary>
/// EN: FlowStep entity owned by AutomationFlow aggregate.
/// VI: FlowStep entity thuộc sở hữu của AutomationFlow aggregate.
/// </summary>
public class FlowStep : Entity
{
    // EN: Private fields for encapsulation
    // VI: Fields private để đóng gói
    private Guid _flowId;
    private int _order;
    private string _action = null!;
    private string _actionConfig = null!;
    private string? _conditions;
    private string? _nextStepMapping;

    /// <summary>
    /// EN: Parent flow ID.
    /// VI: ID của flow cha.
    /// </summary>
    public Guid FlowId => _flowId;

    /// <summary>
    /// EN: Step execution order.
    /// VI: Thứ tự thực thi của bước.
    /// </summary>
    public int Order => _order;

    /// <summary>
    /// EN: Step action type.
    /// VI: Loại action của bước.
    /// </summary>
    public string Action => _action;

    /// <summary>
    /// EN: Action configuration as JSON.
    /// VI: Cấu hình action dạng JSON.
    /// </summary>
    public string ActionConfig => _actionConfig;

    /// <summary>
    /// EN: Conditions for conditional branching as JSON.
    /// VI: Điều kiện cho phân nhánh dạng JSON.
    /// </summary>
    public string? Conditions => _conditions;

    /// <summary>
    /// EN: Next step mapping for branching as JSON.
    /// VI: Mapping bước tiếp theo cho phân nhánh dạng JSON.
    /// </summary>
    public string? NextStepMapping => _nextStepMapping;

    /// <summary>
    /// EN: Valid action types.
    /// VI: Các loại action hợp lệ.
    /// </summary>
    public static class Actions
    {
        public const string SendMessage = "send_message";
        public const string QuickReply = "quick_reply";
        public const string TagCustomer = "tag_customer";
        public const string CallWebhook = "call_webhook";
        public const string AssignToAgent = "assign_to_agent";
        public const string Delay = "delay";
        public const string ConditionalBranch = "conditional_branch";
    }

    /// <summary>
    /// EN: Private constructor for EF Core.
    /// VI: Constructor private cho EF Core.
    /// </summary>
    protected FlowStep()
    {
    }

    /// <summary>
    /// EN: Create a new flow step.
    /// VI: Tạo một bước flow mới.
    /// </summary>
    internal FlowStep(Guid flowId, int order, string action, object actionConfig, object? conditions = null, object? nextStepMapping = null) : this()
    {
        var validActions = new[] { Actions.SendMessage, Actions.QuickReply, Actions.TagCustomer, Actions.CallWebhook, Actions.AssignToAgent, Actions.Delay, Actions.ConditionalBranch };
        if (!validActions.Contains(action.ToLowerInvariant()))
            throw new ArgumentException($"Invalid action type. Valid values: {string.Join(", ", validActions)}");

        Id = Guid.NewGuid();
        _flowId = flowId;
        _order = order;
        _action = action.ToLowerInvariant();
        _actionConfig = JsonSerializer.Serialize(actionConfig);
        _conditions = conditions != null ? JsonSerializer.Serialize(conditions) : null;
        _nextStepMapping = nextStepMapping != null ? JsonSerializer.Serialize(nextStepMapping) : null;
    }

    /// <summary>
    /// EN: Update step configuration.
    /// VI: Cập nhật cấu hình bước.
    /// </summary>
    internal void Update(object actionConfig, object? conditions = null, object? nextStepMapping = null)
    {
        _actionConfig = JsonSerializer.Serialize(actionConfig);
        _conditions = conditions != null ? JsonSerializer.Serialize(conditions) : null;
        _nextStepMapping = nextStepMapping != null ? JsonSerializer.Serialize(nextStepMapping) : null;
    }

    /// <summary>
    /// EN: Update step order.
    /// VI: Cập nhật thứ tự bước.
    /// </summary>
    internal void SetOrder(int order)
    {
        _order = order;
    }
}
