using FluentValidation;
using MktXService.API.Application.Commands;

namespace MktXService.API.Application.Validations;

/// <summary>
/// EN: Validator for CreateCampaignCommand.
/// VI: Validator cho CreateCampaignCommand.
/// </summary>
public class CreateCampaignCommandValidator : AbstractValidator<CreateCampaignCommand>
{
    public CreateCampaignCommandValidator()
    {
        RuleFor(x => x.MerchantId)
            .NotEmpty()
            .WithMessage("Merchant ID is required / Merchant ID là bắt buộc");

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

        RuleFor(x => x.Type)
            .NotEmpty()
            .WithMessage("Campaign type is required / Loại chiến dịch là bắt buộc")
            .Must(BeValidCampaignType)
            .WithMessage("Invalid campaign type / Loại chiến dịch không hợp lệ");

        RuleFor(x => x.SegmentIds)
            .NotEmpty()
            .WithMessage("At least one segment is required / Cần ít nhất một phân khúc");
    }

    private static bool BeValidCampaignType(string type)
    {
        var validTypes = new[] { "bulk_dm", "welcome", "follow_up", "drip", "broadcast" };
        return validTypes.Contains(type.ToLowerInvariant());
    }
}

/// <summary>
/// EN: Validator for StartCampaignCommand.
/// VI: Validator cho StartCampaignCommand.
/// </summary>
public class StartCampaignCommandValidator : AbstractValidator<StartCampaignCommand>
{
    public StartCampaignCommandValidator()
    {
        RuleFor(x => x.CampaignId)
            .NotEmpty()
            .WithMessage("Campaign ID is required / Campaign ID là bắt buộc");
    }
}

/// <summary>
/// EN: Validator for PauseCampaignCommand.
/// VI: Validator cho PauseCampaignCommand.
/// </summary>
public class PauseCampaignCommandValidator : AbstractValidator<PauseCampaignCommand>
{
    public PauseCampaignCommandValidator()
    {
        RuleFor(x => x.CampaignId)
            .NotEmpty()
            .WithMessage("Campaign ID is required / Campaign ID là bắt buộc");
    }
}

/// <summary>
/// EN: Validator for ResumeCampaignCommand.
/// VI: Validator cho ResumeCampaignCommand.
/// </summary>
public class ResumeCampaignCommandValidator : AbstractValidator<ResumeCampaignCommand>
{
    public ResumeCampaignCommandValidator()
    {
        RuleFor(x => x.CampaignId)
            .NotEmpty()
            .WithMessage("Campaign ID is required / Campaign ID là bắt buộc");
    }
}

/// <summary>
/// EN: Validator for CancelCampaignCommand.
/// VI: Validator cho CancelCampaignCommand.
/// </summary>
public class CancelCampaignCommandValidator : AbstractValidator<CancelCampaignCommand>
{
    public CancelCampaignCommandValidator()
    {
        RuleFor(x => x.CampaignId)
            .NotEmpty()
            .WithMessage("Campaign ID is required / Campaign ID là bắt buộc");

        RuleFor(x => x.Reason)
            .MaximumLength(500)
            .WithMessage("Reason must be less than 500 characters / Lý do phải ít hơn 500 ký tự")
            .When(x => x.Reason != null);
    }
}

/// <summary>
/// EN: Validator for UpdateCampaignMetricsCommand.
/// VI: Validator cho UpdateCampaignMetricsCommand.
/// </summary>
public class UpdateCampaignMetricsCommandValidator : AbstractValidator<UpdateCampaignMetricsCommand>
{
    public UpdateCampaignMetricsCommandValidator()
    {
        RuleFor(x => x.CampaignId)
            .NotEmpty()
            .WithMessage("Campaign ID is required / Campaign ID là bắt buộc");

        RuleFor(x => x.Sent)
            .GreaterThanOrEqualTo(0)
            .WithMessage("Sent count cannot be negative / Số lượng gửi không thể âm");

        RuleFor(x => x.Delivered)
            .GreaterThanOrEqualTo(0)
            .WithMessage("Delivered count cannot be negative / Số lượng delivered không thể âm");

        RuleFor(x => x.Failed)
            .GreaterThanOrEqualTo(0)
            .WithMessage("Failed count cannot be negative / Số lượng failed không thể âm");
    }
}
