using FluentValidation;
using Microsoft.Extensions.Options;
using SatelliteProvider.Common.Configs;
using SatelliteProvider.Common.DTO;

namespace SatelliteProvider.Api.Validators;

// AZ-810: root validator for the UAV upload metadata envelope. Runs from
// inside the custom `UavUploadValidationFilter` (the endpoint takes a
// multipart form, so the standard `WithValidation<T>()` JSON-body filter
// doesn't apply). Error keys come out as `errors.items[…]` from this
// validator and are prefixed with `metadata.` by the filter, producing
// `errors.metadata.items[…]` in the final ValidationProblemDetails per
// `_docs/02_document/contracts/api/error-shape.md` v1.0.0.
public sealed class UavTileBatchMetadataPayloadValidator : AbstractValidator<UavTileBatchMetadataPayload>
{
    public UavTileBatchMetadataPayloadValidator(
        IOptions<UavQualityConfig> qualityConfig,
        TimeProvider? timeProvider = null)
    {
        ArgumentNullException.ThrowIfNull(qualityConfig);
        var maxBatchSize = qualityConfig.Value.MaxBatchSize;

        RuleFor(p => p.Items)
            .NotNull().WithMessage("`items` is required.")
            .NotEmpty().WithMessage("`items` must contain at least one entry.")
            .Must(items => items is null || items.Count <= maxBatchSize)
                .WithMessage($"`items` must contain at most {maxBatchSize} entries.");

        RuleForEach(p => p.Items)
            .SetValidator(new UavTileMetadataValidator(qualityConfig, timeProvider));
    }
}
