using Core.Abstractions.Models;
using Core.DomainObjects;
using Core.DomainObjects.Documents;
using Core.Enumerations;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;

namespace WeatherForecast.Models.Requests {

  /// <summary>
  /// 
  /// </summary>
  public class ShopCartItemRequestModel : RequestModelBase<ShopCartItem> {

    /// <summary>
    /// 
    /// </summary>
    public uint? Quantity { get; set; }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public override ShopCartItem ToDomainObject() {
      if (HasValidationErrors()) throw new ValidationException();

      return new ShopCartItem() {
        Quantity = Quantity.Value,
        Created = DateTime.UtcNow
      };
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="validationContext"></param>
    /// <returns></returns>
    public override IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
      if (Quantity == null || (Quantity != null && Quantity == 0))
        yield return new ValidationResult($"{nameof(Quantity)} {Errors.NullOrEmpty}");
    }

    [MemberNotNullWhen(false, new[] { nameof(Quantity) })]
    private bool HasValidationErrors() => Validate(new ValidationContext(this)).Any();
  }
}
