using FluentValidation;
using FacebookService.API.Application.Commands;

namespace FacebookService.API.Application.Validations;

/// <summary>
/// EN: Validator for ProcessIncomingMessageCommand.
/// VI: Validator cho ProcessIncomingMessageCommand.
/// </summary>
public class ProcessIncomingMessageCommandValidator : AbstractValidator<ProcessIncomingMessageCommand>
{
    public ProcessIncomingMessageCommandValidator()
    {
        RuleFor(x => x.PageId)
            .NotEmpty()
            .WithMessage("Page ID is required / Page ID là bắt buộc")
            .MaximumLength(50)
            .WithMessage("Page ID must be less than 50 characters / Page ID phải ít hơn 50 ký tự");

        RuleFor(x => x.SenderId)
            .NotEmpty()
            .WithMessage("Sender ID is required / Sender ID là bắt buộc")
            .MaximumLength(50)
            .WithMessage("Sender ID must be less than 50 characters / Sender ID phải ít hơn 50 ký tự");

        RuleFor(x => x.MessageText)
            .NotEmpty()
            .WithMessage("Message text is required / Nội dung tin nhắn là bắt buộc");
    }
}

/// <summary>
/// EN: Validator for CreateChatbotFlowCommand.
/// VI: Validator cho CreateChatbotFlowCommand.
/// </summary>
public class CreateChatbotFlowCommandValidator : AbstractValidator<CreateChatbotFlowCommand>
{
    private static readonly string[] ValidTriggerTypes = { "GetStarted", "Keyword", "Postback", "Fallback" };

    public CreateChatbotFlowCommandValidator()
    {
        RuleFor(x => x.ShopId)
            .NotEmpty()
            .WithMessage("Shop ID is required / Shop ID là bắt buộc");

        RuleFor(x => x.Name)
            .NotEmpty()
            .WithMessage("Flow name is required / Tên flow là bắt buộc")
            .MaximumLength(255)
            .WithMessage("Flow name must be less than 255 characters / Tên flow phải ít hơn 255 ký tự");

        RuleFor(x => x.TriggerType)
            .NotEmpty()
            .WithMessage("Trigger type is required / Loại trigger là bắt buộc")
            .Must(x => ValidTriggerTypes.Contains(x, StringComparer.OrdinalIgnoreCase))
            .WithMessage($"Trigger type must be one of: {string.Join(", ", ValidTriggerTypes)}");

        RuleFor(x => x.TriggerValue)
            .NotEmpty()
            .WithMessage("Trigger value is required / Giá trị trigger là bắt buộc")
            .MaximumLength(255)
            .WithMessage("Trigger value must be less than 255 characters / Giá trị trigger phải ít hơn 255 ký tự");
    }
}

/// <summary>
/// EN: Validator for UpsertAIChatbotConfigCommand.
/// VI: Validator cho UpsertAIChatbotConfigCommand.
/// </summary>
public class UpsertAIChatbotConfigCommandValidator : AbstractValidator<UpsertAIChatbotConfigCommand>
{
    private static readonly string[] ValidProviders = { "OpenAI", "AzureOpenAI", "Azure" };

    public UpsertAIChatbotConfigCommandValidator()
    {
        RuleFor(x => x.ShopId)
            .NotEmpty()
            .WithMessage("Shop ID is required / Shop ID là bắt buộc");

        RuleFor(x => x.Provider)
            .NotEmpty()
            .WithMessage("Provider is required / Provider là bắt buộc")
            .Must(x => ValidProviders.Contains(x, StringComparer.OrdinalIgnoreCase))
            .WithMessage($"Provider must be one of: {string.Join(", ", ValidProviders)}");

        RuleFor(x => x.Model)
            .NotEmpty()
            .WithMessage("Model is required / Model là bắt buộc")
            .MaximumLength(50)
            .WithMessage("Model must be less than 50 characters / Model phải ít hơn 50 ký tự");

        RuleFor(x => x.SystemPrompt)
            .NotEmpty()
            .WithMessage("System prompt is required / System prompt là bắt buộc");

        RuleFor(x => x.Temperature)
            .InclusiveBetween(0.0f, 2.0f)
            .WithMessage("Temperature must be between 0.0 and 2.0 / Temperature phải từ 0.0 đến 2.0");

        RuleFor(x => x.MaxTokens)
            .InclusiveBetween(1, 4096)
            .WithMessage("Max tokens must be between 1 and 4096 / Max tokens phải từ 1 đến 4096");
    }
}
