using FluentValidation;
using Monitoring.Core.Domain.AssetManagement;

namespace Monitoring.Features.Assets.UpdateAsset;

public class UpdateAssetValidator : AbstractValidator<UpdateAssetRequest>
{
	public UpdateAssetValidator()
	{
		_ = RuleFor(x => x.GroupIds).NotNull();

		_ = RuleFor(x => x.Name)
			.NotEmpty()
			.MinimumLength(AssetName.MinLength)
			.MaximumLength(AssetName.MaxLength);

		_ = RuleFor(x => x.Description)
			.NotEmpty()
			.MinimumLength(AssetDescription.MinLength)
			.MaximumLength(AssetDescription.MaxLength);

		_ = RuleFor(x => x.Comment)
			.MaximumLength(AssetComment.MaxLength)
			.When(x => !string.IsNullOrEmpty(x.Comment));
		
		_ = RuleFor(x => x.Latitude)
			.GreaterThanOrEqualTo(-90)
			.LessThanOrEqualTo(90)
			.When(x => x.Latitude.HasValue);

		_ = RuleFor(x => x.Longitude)
			.GreaterThanOrEqualTo(-180)
			.LessThanOrEqualTo(180)
			.When(x => x.Longitude.HasValue);

		_ = RuleFor(x => x.RowVersion).NotNull().NotEmpty();

		_ = RuleFor(x => x.Imei)
			.Length(15, 16)
			.Matches(@"^\d+$")
			.WithMessage("IMEI должен содержать только цифры и быть длиной 15-16 символов")
			.When(x => !string.IsNullOrWhiteSpace(x.Imei));
	}
}
