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

namespace Monitoring.Features.Assets.CreateAsset;

public class CreateAssetValidator : AbstractValidator<CreateAssetRequest>
{
	public CreateAssetValidator()
	{
		_ = RuleFor(x => x.OrganizationId).NotEmpty();

		_ = RuleFor(x => x.ServiceOrganizationId).NotEmpty()
       		.When(x => x.ServiceOrganizationId.HasValue);

		_ = 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);

		_ = 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);

		// TODO: add for all fields
		_ = RuleFor(x => x.Apartment).MaximumLength(AssetName.MaxLength);
		_ = RuleFor(x => x.Building).MaximumLength(AssetName.MaxLength);
		_ = RuleFor(x => x.Country).MaximumLength(AssetName.MaxLength);
		_ = RuleFor(x => x.City).MaximumLength(AssetName.MaxLength);
		_ = RuleFor(x => x.Region).MaximumLength(AssetName.MaxLength);
		_ = RuleFor(x => x.Street).MaximumLength(AssetName.MaxLength);
		_ = RuleFor(x => x.House).MaximumLength(AssetName.MaxLength);
		_ = RuleFor(x => x.GateCode).MaximumLength(AssetName.MaxLength);
		_ = RuleFor(x => x.IntercomCode).MaximumLength(AssetName.MaxLength);
		_ = RuleFor(x => x.OwnerName).MaximumLength(AssetName.MaxLength);
		_ = RuleFor(x => x.OwnerPassword).MaximumLength(AssetName.MaxLength);
		_ = RuleFor(x => x.OwnerPhone1).MaximumLength(AssetName.MaxLength);
		_ = RuleFor(x => x.OwnerPhone2).MaximumLength(AssetName.MaxLength);
		_ = RuleFor(x => x.IdentificationPassword).MaximumLength(AssetName.MaxLength);
		_ = RuleFor(x => x.DuressPassword).MaximumLength(AssetName.MaxLength);

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