using FluentValidation;

namespace ColaFlow.Modules.ProjectManagement.Application.Commands.UpdateTask;

/// <summary>
/// Validator for UpdateTaskCommand
/// </summary>
public sealed class UpdateTaskCommandValidator : AbstractValidator<UpdateTaskCommand>
{
    public UpdateTaskCommandValidator()
    {
        RuleFor(x => x.TaskId)
            .NotEmpty()
            .WithMessage("TaskId is required");

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

        RuleFor(x => x.Description)
            .MaximumLength(5000)
            .WithMessage("Description cannot exceed 5000 characters");

        RuleFor(x => x.Priority)
            .Must(BeValidPriority)
            .When(x => !string.IsNullOrWhiteSpace(x.Priority))
            .WithMessage("Priority must be one of: Low, Medium, High, Urgent");

        RuleFor(x => x.Status)
            .Must(BeValidStatus)
            .When(x => !string.IsNullOrWhiteSpace(x.Status))
            .WithMessage("Status must be one of: ToDo, InProgress, Done, Blocked");

        RuleFor(x => x.EstimatedHours)
            .GreaterThanOrEqualTo(0)
            .When(x => x.EstimatedHours.HasValue)
            .WithMessage("Estimated hours cannot be negative");
    }

    private bool BeValidPriority(string? priority)
    {
        if (string.IsNullOrWhiteSpace(priority)) return true;
        var validPriorities = new[] { "Low", "Medium", "High", "Urgent" };
        return validPriorities.Contains(priority, StringComparer.OrdinalIgnoreCase);
    }

    private bool BeValidStatus(string? status)
    {
        if (string.IsNullOrWhiteSpace(status)) return true;
        var validStatuses = new[] { "ToDo", "InProgress", "Done", "Blocked" };
        return validStatuses.Contains(status, StringComparer.OrdinalIgnoreCase);
    }
}
