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

using DomainResults.Mvc;

using WeatherForecast.Services;
using WeatherForecast.Policies;
using Core.Enumerations;
using DataProviders.Collections;

namespace WeatherForecast.Controllers;

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

  private readonly IAuthorizationService _authorizationService;
  private readonly IShopCartDataProvider _shopCartDataProvider;
  private readonly IShopCartItemsService _shopCartItemsService;

  /// <summary>
  /// 
  /// </summary>
  /// <param name="authorizationService"></param>
  /// <param name="shopCartDataProvider"></param>
  /// <param name="shopCartItemsService"></param>
  public ShopCartItemsController(
    IAuthorizationService authorizationService,
    IShopCartDataProvider shopCartDataProvider,
    IShopCartItemsService shopCartItemsService
  ) {
    _authorizationService = authorizationService;
    _shopCartDataProvider = shopCartDataProvider;
    _shopCartItemsService = shopCartItemsService;
  }

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

    var (cartItems, getCartItemsResult) = _shopCartDataProvider.GetAll(siteId, userId);
    if (!getCartItemsResult.IsSuccess || cartItems == null)
      return getCartItemsResult.ToActionResult();

    if ((await _authorizationService.AuthorizeAsync(User, cartItems, new ShopCartAuthorizationRequirement {
      Action = CrudActions.Read
    })).Succeeded) {
      var result = _shopCartItemsService.Get(cartItems, locale);
      return result.ToActionResult();
    }

    return Unauthorized();
  }

  //[HttpDelete("{siteId}/{userId}")]
  //public async Task<IActionResult> Delete([FromRoute] Guid siteId, [FromRoute] Guid userId) {

  //  if ((await _authorizationService.AuthorizeAsync(User, null, new ShopCartAuthorizationRequirement {
  //    WhiteListToken = true,
  //    SameAuthor = true,
  //    Action = CrudActions.Delete
  //  })).Succeeded) {
  //    var result = _shopCartItemsService.Delete(siteId, userId);
  //    return result.ToActionResult();
  //  }

  //  return Unauthorized();
  //}

}
