using WhatsAppService.Domain.SeedWork;

namespace WhatsAppService.Domain.AggregatesModel.AIAgentAggregate;

/// <summary>
/// EN: Value object representing AI agent personality configuration.
/// VI: Value object đại diện cho cấu hình tính cách AI agent.
/// </summary>
public class AgentPersonality : ValueObject
{
    /// <summary>
    /// EN: Agent tone (friendly, professional, casual).
    /// VI: Giọng điệu agent (friendly, professional, casual).
    /// </summary>
    public string Tone { get; private set; }

    /// <summary>
    /// EN: Primary language (vietnamese, english).
    /// VI: Ngôn ngữ chính (vietnamese, english).
    /// </summary>
    public string Language { get; private set; }

    /// <summary>
    /// EN: List of constraints/restrictions.
    /// VI: Danh sách các ràng buộc/hạn chế.
    /// </summary>
    public IReadOnlyList<string> Constraints { get; private set; }

    /// <summary>
    /// EN: Custom prompt template.
    /// VI: Template prompt tùy chỉnh.
    /// </summary>
    public string? PromptTemplate { get; private set; }

    /// <summary>
    /// EN: Valid tone values.
    /// VI: Các giá trị giọng điệu hợp lệ.
    /// </summary>
    public static class Tones
    {
        public const string Friendly = "friendly";
        public const string Professional = "professional";
        public const string Casual = "casual";
    }

    /// <summary>
    /// EN: Valid language values.
    /// VI: Các giá trị ngôn ngữ hợp lệ.
    /// </summary>
    public static class Languages
    {
        public const string Vietnamese = "vietnamese";
        public const string English = "english";
    }

    /// <summary>
    /// EN: Private constructor for EF Core.
    /// VI: Constructor private cho EF Core.
    /// </summary>
    private AgentPersonality()
    {
        Tone = Tones.Friendly;
        Language = Languages.Vietnamese;
        Constraints = new List<string>();
    }

    /// <summary>
    /// EN: Create a new AgentPersonality value object.
    /// VI: Tạo một AgentPersonality value object mới.
    /// </summary>
    public AgentPersonality(
        string tone,
        string language,
        IEnumerable<string>? constraints = null,
        string? promptTemplate = null)
    {
        var validTones = new[] { Tones.Friendly, Tones.Professional, Tones.Casual };
        if (!validTones.Contains(tone.ToLowerInvariant()))
            throw new ArgumentException($"Invalid tone. Valid values: {string.Join(", ", validTones)}");

        var validLanguages = new[] { Languages.Vietnamese, Languages.English };
        if (!validLanguages.Contains(language.ToLowerInvariant()))
            throw new ArgumentException($"Invalid language. Valid values: {string.Join(", ", validLanguages)}");

        Tone = tone.ToLowerInvariant();
        Language = language.ToLowerInvariant();
        Constraints = constraints?.ToList() ?? new List<string>();
        PromptTemplate = promptTemplate;
    }

    /// <summary>
    /// EN: Create default friendly Vietnamese personality.
    /// VI: Tạo tính cách thân thiện tiếng Việt mặc định.
    /// </summary>
    public static AgentPersonality CreateDefault() => new(Tones.Friendly, Languages.Vietnamese);

    protected override IEnumerable<object> GetEqualityComponents()
    {
        yield return Tone;
        yield return Language;
        foreach (var constraint in Constraints)
        {
            yield return constraint;
        }
        yield return PromptTemplate ?? string.Empty;
    }
}
