// EN: Validators for Catalog commands (products, categories).
// VI: Validators cho cac commands Catalog (san pham, danh muc).

using FluentValidation;
using CatalogService.API.Application.Commands;

namespace CatalogService.API.Application.Validations;

/// <summary>
/// EN: Validator for CreateProductCommand.
/// VI: Validator cho CreateProductCommand.
/// </summary>
public class CreateProductCommandValidator : AbstractValidator<CreateProductCommand>
{
    private static readonly string[] ValidProductTypes = ["Physical", "Service", "PreparedFood"];

    public CreateProductCommandValidator()
    {
        RuleFor(x => x.ShopId)
            .NotEmpty()
            .WithMessage("Shop ID is required / Shop ID la bat buoc");

        RuleFor(x => x.Name)
            .NotEmpty()
            .WithMessage("Product name is required / Ten san pham la bat buoc")
            .MaximumLength(200)
            .WithMessage("Product name must not exceed 200 characters / Ten san pham khong vuot qua 200 ky tu");

        RuleFor(x => x.Description)
            .MaximumLength(2000)
            .WithMessage("Description must not exceed 2000 characters / Mo ta khong vuot qua 2000 ky tu")
            .When(x => x.Description != null);

        RuleFor(x => x.Price)
            .GreaterThanOrEqualTo(0)
            .WithMessage("Price must not be negative / Gia khong duoc am");

        RuleFor(x => x.Type)
            .NotEmpty()
            .WithMessage("Product type is required / Loai san pham la bat buoc")
            .Must(t => ValidProductTypes.Contains(t))
            .WithMessage("Invalid product type. Valid values: Physical, Service, PreparedFood / Loai san pham khong hop le");

        RuleFor(x => x.Sku)
            .MaximumLength(100)
            .WithMessage("SKU must not exceed 100 characters / Ma SKU khong vuot qua 100 ky tu")
            .When(x => x.Sku != null);

        RuleFor(x => x.ImageUrl)
            .MaximumLength(2048)
            .WithMessage("Image URL must not exceed 2048 characters / URL hinh anh khong vuot qua 2048 ky tu")
            .When(x => x.ImageUrl != null);
    }
}

/// <summary>
/// EN: Validator for UpdateProductCommand.
/// VI: Validator cho UpdateProductCommand.
/// </summary>
public class UpdateProductCommandValidator : AbstractValidator<UpdateProductCommand>
{
    public UpdateProductCommandValidator()
    {
        RuleFor(x => x.ProductId)
            .NotEmpty()
            .WithMessage("Product ID is required / Product ID la bat buoc");

        RuleFor(x => x.Name)
            .NotEmpty()
            .WithMessage("Product name is required / Ten san pham la bat buoc")
            .MaximumLength(200)
            .WithMessage("Product name must not exceed 200 characters / Ten san pham khong vuot qua 200 ky tu");

        RuleFor(x => x.Description)
            .MaximumLength(2000)
            .WithMessage("Description must not exceed 2000 characters / Mo ta khong vuot qua 2000 ky tu")
            .When(x => x.Description != null);

        RuleFor(x => x.Price)
            .GreaterThanOrEqualTo(0)
            .WithMessage("Price must not be negative / Gia khong duoc am");

        RuleFor(x => x.ImageUrl)
            .MaximumLength(2048)
            .WithMessage("Image URL must not exceed 2048 characters / URL hinh anh khong vuot qua 2048 ky tu")
            .When(x => x.ImageUrl != null);
    }
}

/// <summary>
/// EN: Validator for DeleteProductCommand.
/// VI: Validator cho DeleteProductCommand.
/// </summary>
public class DeleteProductCommandValidator : AbstractValidator<DeleteProductCommand>
{
    public DeleteProductCommandValidator()
    {
        RuleFor(x => x.ProductId)
            .NotEmpty()
            .WithMessage("Product ID is required / Product ID la bat buoc");
    }
}

/// <summary>
/// EN: Validator for CreateCategoryCommand.
/// VI: Validator cho CreateCategoryCommand.
/// </summary>
public class CreateCategoryCommandValidator : AbstractValidator<CreateCategoryCommand>
{
    public CreateCategoryCommandValidator()
    {
        RuleFor(x => x.ShopId)
            .NotEmpty()
            .WithMessage("Shop ID is required / Shop ID la bat buoc");

        RuleFor(x => x.Name)
            .NotEmpty()
            .WithMessage("Category name is required / Ten danh muc la bat buoc")
            .MaximumLength(200)
            .WithMessage("Category name must not exceed 200 characters / Ten danh muc khong vuot qua 200 ky tu");

        RuleFor(x => x.Description)
            .MaximumLength(1000)
            .WithMessage("Description must not exceed 1000 characters / Mo ta khong vuot qua 1000 ky tu")
            .When(x => x.Description != null);

        RuleFor(x => x.DisplayOrder)
            .GreaterThanOrEqualTo(0)
            .WithMessage("Display order must not be negative / Thu tu hien thi khong duoc am");

        RuleFor(x => x.ImageUrl)
            .MaximumLength(2048)
            .WithMessage("Image URL must not exceed 2048 characters / URL hinh anh khong vuot qua 2048 ky tu")
            .When(x => x.ImageUrl != null);
    }
}

/// <summary>
/// EN: Validator for UpdateCategoryCommand.
/// VI: Validator cho UpdateCategoryCommand.
/// </summary>
public class UpdateCategoryCommandValidator : AbstractValidator<UpdateCategoryCommand>
{
    public UpdateCategoryCommandValidator()
    {
        RuleFor(x => x.CategoryId)
            .NotEmpty()
            .WithMessage("Category ID is required / Category ID la bat buoc");

        RuleFor(x => x.Name)
            .NotEmpty()
            .WithMessage("Category name is required / Ten danh muc la bat buoc")
            .MaximumLength(200)
            .WithMessage("Category name must not exceed 200 characters / Ten danh muc khong vuot qua 200 ky tu");

        RuleFor(x => x.Description)
            .MaximumLength(1000)
            .WithMessage("Description must not exceed 1000 characters / Mo ta khong vuot qua 1000 ky tu")
            .When(x => x.Description != null);

        RuleFor(x => x.DisplayOrder)
            .GreaterThanOrEqualTo(0)
            .WithMessage("Display order must not be negative / Thu tu hien thi khong duoc am");

        RuleFor(x => x.ImageUrl)
            .MaximumLength(2048)
            .WithMessage("Image URL must not exceed 2048 characters / URL hinh anh khong vuot qua 2048 ky tu")
            .When(x => x.ImageUrl != null);
    }
}

/// <summary>
/// EN: Validator for DeleteCategoryCommand.
/// VI: Validator cho DeleteCategoryCommand.
/// </summary>
public class DeleteCategoryCommandValidator : AbstractValidator<DeleteCategoryCommand>
{
    public DeleteCategoryCommandValidator()
    {
        RuleFor(x => x.CategoryId)
            .NotEmpty()
            .WithMessage("Category ID is required / Category ID la bat buoc");
    }
}
