using FacebookService.Domain.SeedWork;

namespace FacebookService.Domain.AggregatesModel.ChatbotAggregate;

/// <summary>
/// EN: Flow node entity representing a single step in an automation flow.
/// VI: Entity node flow đại diện cho một bước trong automation flow.
/// </summary>
public class FlowNode : Entity
{
    // EN: Private fields for encapsulation
    // VI: Fields private để đóng gói
    private NodeType _nodeType = null!;
    private string? _content;
    private Dictionary<string, object> _config = new();
    private List<Guid> _nextNodeIds = new();
    private int _orderIndex;

    /// <summary>
    /// EN: Node type. / VI: Loại node.
    /// </summary>
    public NodeType NodeType => _nodeType;

    /// <summary>
    /// EN: Node type ID for EF Core. / VI: ID loại node cho EF Core.
    /// </summary>
    public int NodeTypeId { get; private set; }

    /// <summary>
    /// EN: Content to display (text, media URL, etc.).
    /// VI: Nội dung hiển thị (text, URL media, v.v.).
    /// </summary>
    public string? Content => _content;

    /// <summary>
    /// EN: Node configuration (quick replies, buttons, conditions, etc.).
    /// VI: Cấu hình node (quick replies, buttons, conditions, v.v.).
    /// </summary>
    public IReadOnlyDictionary<string, object> Config => _config.AsReadOnly();

    /// <summary>
    /// EN: IDs of next nodes to execute.
    /// VI: IDs của các nodes tiếp theo cần thực thi.
    /// </summary>
    public IReadOnlyCollection<Guid> NextNodeIds => _nextNodeIds.AsReadOnly();

    /// <summary>
    /// EN: Order index for display. / VI: Thứ tự hiển thị.
    /// </summary>
    public int OrderIndex => _orderIndex;

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

    /// <summary>
    /// EN: Create a new flow node.
    /// VI: Tạo flow node mới.
    /// </summary>
    public FlowNode(
        NodeType nodeType,
        string? content = null,
        int orderIndex = 0,
        Dictionary<string, object>? config = null) : this()
    {
        Id = Guid.NewGuid();
        _nodeType = nodeType;
        NodeTypeId = nodeType.Id;
        _content = content;
        _orderIndex = orderIndex;
        _config = config ?? new Dictionary<string, object>();
    }

    /// <summary>
    /// EN: Add a connection to another node.
    /// VI: Thêm kết nối đến node khác.
    /// </summary>
    public void ConnectTo(Guid nextNodeId)
    {
        if (!_nextNodeIds.Contains(nextNodeId))
        {
            _nextNodeIds.Add(nextNodeId);
        }
    }

    /// <summary>
    /// EN: Remove a connection to another node.
    /// VI: Xóa kết nối đến node khác.
    /// </summary>
    public void DisconnectFrom(Guid nextNodeId)
    {
        _nextNodeIds.Remove(nextNodeId);
    }

    /// <summary>
    /// EN: Update node content.
    /// VI: Cập nhật nội dung node.
    /// </summary>
    public void UpdateContent(string? content)
    {
        _content = content;
    }

    /// <summary>
    /// EN: Update node configuration.
    /// VI: Cập nhật cấu hình node.
    /// </summary>
    public void UpdateConfig(Dictionary<string, object> config)
    {
        _config = config ?? new Dictionary<string, object>();
    }

    /// <summary>
    /// EN: Set config value.
    /// VI: Đặt giá trị config.
    /// </summary>
    public void SetConfigValue(string key, object value)
    {
        _config[key] = value;
    }
}
