using FluentValidation;
using IamService.API.Application.Commands.Organizations;

namespace IamService.API.Application.Validations;

/// <summary>
/// EN: Validator for CreateOrganizationCommand.
/// VI: Validator cho CreateOrganizationCommand.
/// </summary>
public class CreateOrganizationCommandValidator : AbstractValidator<CreateOrganizationCommand>
{
    public CreateOrganizationCommandValidator()
    {
        RuleFor(x => x.Name)
            .NotEmpty().WithMessage("Organization name is required")
            .MaximumLength(200).WithMessage("Organization name cannot exceed 200 characters")
            .Matches(@"^[\w\s\-\.]+$").WithMessage("Organization name contains invalid characters");

        RuleFor(x => x.Slug)
            .NotEmpty().WithMessage("Slug is required")
            .MaximumLength(100).WithMessage("Slug cannot exceed 100 characters")
            .Matches(@"^[a-z0-9\-]+$").WithMessage("Slug must be lowercase letters, numbers, and hyphens only")
            .Must(slug => !slug.StartsWith("-") && !slug.EndsWith("-"))
            .WithMessage("Slug cannot start or end with a hyphen");

        RuleFor(x => x.Description)
            .MaximumLength(2000).WithMessage("Description cannot exceed 2000 characters")
            .When(x => x.Description != null);

        RuleFor(x => x.ParentOrganizationId)
            .Must(id => id == null || id != Guid.Empty)
            .WithMessage("Parent organization ID cannot be empty GUID");
    }
}

/// <summary>
/// EN: Validator for UpdateOrganizationCommand.
/// VI: Validator cho UpdateOrganizationCommand.
/// </summary>
public class UpdateOrganizationCommandValidator : AbstractValidator<UpdateOrganizationCommand>
{
    public UpdateOrganizationCommandValidator()
    {
        RuleFor(x => x.Id)
            .NotEmpty().WithMessage("Organization ID is required");

        RuleFor(x => x.Name)
            .NotEmpty().WithMessage("Organization name is required")
            .MaximumLength(200).WithMessage("Organization name cannot exceed 200 characters");

        RuleFor(x => x.Description)
            .MaximumLength(2000).WithMessage("Description cannot exceed 2000 characters")
            .When(x => x.Description != null);
    }
}

/// <summary>
/// EN: Validator for ArchiveOrganizationCommand.
/// VI: Validator cho ArchiveOrganizationCommand.
/// </summary>
public class ArchiveOrganizationCommandValidator : AbstractValidator<ArchiveOrganizationCommand>
{
    public ArchiveOrganizationCommandValidator()
    {
        RuleFor(x => x.Id)
            .NotEmpty().WithMessage("Organization ID is required");
    }
}
