using FluentValidation;
using LittleShop.DTOs;

namespace LittleShop.Validators;

public class CreateOrderDtoValidator : AbstractValidator<CreateOrderDto>
{
    public CreateOrderDtoValidator()
    {
        RuleFor(x => x.IdentityReference)
            .NotEmpty().WithMessage("Identity reference is required")
            .MaximumLength(100).WithMessage("Identity reference cannot exceed 100 characters");
            
        RuleFor(x => x.ShippingName)
            .NotEmpty().WithMessage("Shipping name is required");
            
        RuleFor(x => x.ShippingAddress)
            .NotEmpty().WithMessage("Shipping address is required");
            
        RuleFor(x => x.ShippingCity)
            .NotEmpty().WithMessage("Shipping city is required");
            
        RuleFor(x => x.ShippingPostCode)
            .NotEmpty().WithMessage("Shipping post code is required");
            
        RuleFor(x => x.ShippingCountry)
            .NotEmpty().WithMessage("Shipping country is required");

        RuleFor(x => x.Items)
            .NotEmpty().WithMessage("Order must contain at least one item");

        RuleForEach(x => x.Items).SetValidator(new CreateOrderItemDtoValidator());
    }
}

public class CreateOrderItemDtoValidator : AbstractValidator<CreateOrderItemDto>
{
    public CreateOrderItemDtoValidator()
    {
        RuleFor(x => x.ProductId)
            .NotEmpty().WithMessage("Product ID is required");

        RuleFor(x => x.Quantity)
            .GreaterThan(0).WithMessage("Quantity must be greater than 0");
    }
}