using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

using DomainResults.Mvc;

using WeatherForecast.Services;
using WeatherForecast.Policies;
using Core.Enumerations;
using DataProviders.Collections;
using DomainObjects.Documents;
using WeatherForecast.Models.Shop.Requests;

namespace WeatherForecast.Controllers;


/// <summary>
/// 
/// </summary>
[AllowAnonymous]
[Route("api/[controller]")]
public class ShopCartItemController : ControllerBase {

  private readonly IAuthorizationService _authorizationService;
  private readonly IShopCartDataProvider _shopCartDataProvider;
  private readonly IShopCartItemService _shopCartItemService;

  /// <summary>
  /// 
  /// </summary>
  /// <param name="authorizationService"></param>
  /// <param name="shopCartDataProvider"></param>
  /// <param name="shopCartItemService"></param>
  public ShopCartItemController(
    IAuthorizationService authorizationService,
    IShopCartDataProvider shopCartDataProvider,
    IShopCartItemService shopCartItemService
  ) {
    _authorizationService = authorizationService;
    _shopCartDataProvider = shopCartDataProvider;
    _shopCartItemService = shopCartItemService;
  }

  /// <summary>
  /// 
  /// </summary>
  /// <param name="siteId"></param>
  /// <param name="userId"></param>
  /// <param name="sku"></param>
  /// <param name="requestData"></param>
  /// <returns></returns>
  [HttpPost("{siteId}/{userId}/{sku}")]
  public async Task<IActionResult> Post([FromRoute] Guid siteId, [FromRoute] Guid userId, [FromRoute] string sku, [FromBody] ShopCartItemRequestModel requestData) {
    if ((await _authorizationService.AuthorizeAsync(User, null, new ShopCartAuthorizationRequirement {
      Action = CrudActions.Create
    })).Succeeded) {
      var result = _shopCartItemService.Post(siteId, userId, sku, requestData);
      return result.ToActionResult();
    }

    return Unauthorized();
  }

  /// <summary>
  /// 
  /// </summary>
  /// <returns></returns>
  [HttpGet("{siteId}/{userId}/{sku}")]
  public async Task<IActionResult> Get([FromRoute] Guid siteId, [FromRoute] Guid userId, [FromRoute] string sku, [FromQuery] string? locale) {

    var (cartItem, getCartItemResult) = _shopCartDataProvider.Get(siteId, userId, sku);
    if (!getCartItemResult.IsSuccess || cartItem == null)
      return getCartItemResult.ToActionResult();

    if ((await _authorizationService.AuthorizeAsync(User, new List<ShopCartDocument> { cartItem }, new ShopCartAuthorizationRequirement {
      Action = CrudActions.Read
    })).Succeeded) {
      var result = _shopCartItemService.Get(cartItem, locale);
      return result.ToActionResult();
    }

    return Unauthorized();
  }

  /// <summary>
  /// 
  /// </summary>
  /// <param name="siteId"></param>
  /// <param name="userId"></param>
  /// <param name="sku"></param>
  /// <param name="requestData"></param>
  /// <returns></returns>
  [HttpPut("{siteId}/{userId}/{sku}")]
  public async Task<IActionResult> Update([FromRoute] Guid siteId, [FromRoute] Guid userId, [FromRoute] string sku, [FromBody] ShopCartItemRequestModel requestData) {

    var (cartItem, getCartItemResult) = _shopCartDataProvider.Get(siteId, userId, sku);
    if (!getCartItemResult.IsSuccess || cartItem == null)
      return getCartItemResult.ToActionResult();

    if ((await _authorizationService.AuthorizeAsync(User, new List<ShopCartDocument> { cartItem }, new ShopCartAuthorizationRequirement {
      Action = CrudActions.Update
    })).Succeeded) {
      var result = _shopCartItemService.Update(cartItem, requestData);
      return result.ToActionResult();
    }

    return Unauthorized();
  }

  /// <summary>
  /// 
  /// </summary>
  /// <param name="siteId"></param>
  /// <param name="userId"></param>
  /// <param name="sku"></param>
  /// <returns></returns>
  [HttpDelete("{siteId}/{userId}/{sku}")]
  public async Task<IActionResult> Delete([FromRoute] Guid siteId, [FromRoute] Guid userId, [FromRoute] string sku) {

    var (cartItem, getCartItemResult) = _shopCartDataProvider.Get(siteId, userId, sku);
    if (!getCartItemResult.IsSuccess || cartItem == null)
      return getCartItemResult.ToActionResult();

    if ((await _authorizationService.AuthorizeAsync(User, new List<ShopCartDocument> { cartItem }, new ShopCartAuthorizationRequirement {
      Action = CrudActions.Delete
    })).Succeeded) {
      var result = _shopCartDataProvider.Delete(cartItem.Id);
      return result.ToActionResult();
    }

    return Unauthorized();
  }
}
