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

using DomainResults.Mvc;

using WeatherForecast.Services;
using WeatherForecast.Models.Requests;
using WeatherForecast.Policies;
using Core.Enumerations;
using DataProviders.Collections;
using DomainObjects.Documents;
using DomainResults.Common;
using ExtensionMethods;

namespace WeatherForecast.Controllers;

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

  private readonly IAuthorizationService _authorizationService;
  private readonly IShopCatalogDataProvider _shopCatalogDataProvider;
  private readonly IShopItemService _shopItemService;

  /// <summary>
  /// 
  /// </summary>
  /// <param name="authorizationService"></param>
  /// <param name="shopCatalogDataProvider"></param>
  /// <param name="shopItemService"></param>
  public ShopItemController(
    IAuthorizationService authorizationService,
    IShopCatalogDataProvider shopCatalogDataProvider,
    IShopItemService shopItemService
  ) {
    _authorizationService = authorizationService;
    _shopCatalogDataProvider = shopCatalogDataProvider;
    _shopItemService = shopItemService;
  }

  #region Authless methods
  /// <summary>
  /// 
  /// </summary>
  /// <param name="siteId"></param>
  /// <param name="slug"></param>
  /// <returns></returns>
  [HttpGet("{siteId}")]
  public IActionResult GetSlug([FromRoute] Guid siteId, [FromQuery] string slug) {
    var result = _shopItemService.GetSlug(siteId, slug);
    return result.ToActionResult();
  }
  #endregion

  /// <summary>
  /// 
  /// </summary>
  /// <param name="siteId"></param>
  /// <param name="sku"></param>
  /// <param name="requestData"></param>
  /// <returns></returns>
  [HttpPost("{siteId}/{sku}")]
  public async Task<IActionResult> Post([FromRoute] Guid siteId, [FromRoute] string sku, [FromBody] ShopItemRequestModel requestData) {
    var shopItem = requestData.ToDomainObject();

    shopItem.SiteId = siteId;
    shopItem.Sku = sku;

    var userId = User?.Identity?.Name?.ToNullableGuid();
    if (userId == null)
      return IDomainResult.Failed().ToActionResult();

    shopItem.Author = userId.Value;

    if ((await _authorizationService.AuthorizeAsync(User, new List<ShopDocument> { shopItem }, new ShopAuthorizationRequirement {
      Action = CrudActions.Create
    })).Succeeded) {

      var result = _shopItemService.Post(shopItem);
      return result.ToActionResult();
    }

    return Unauthorized();
  }

  /// <summary>
  /// Returns full object
  /// </summary>
  /// <param name="siteId"></param>
  /// <param name="sku"></param>
  /// <returns></returns>
  [HttpGet("{siteId}/{sku}")]
  public async Task<IActionResult> Get([FromRoute] Guid siteId, [FromRoute] string sku) {

    var (shopItem, getShopItemResult) = _shopCatalogDataProvider.Get(siteId, sku);
    if (!getShopItemResult.IsSuccess || shopItem == null)
      return getShopItemResult.ToActionResult();

    if ((await _authorizationService.AuthorizeAsync(User, new List<ShopDocument> { shopItem }, new ShopAuthorizationRequirement {
      Action = CrudActions.Read
    })).Succeeded) {
      var result = _shopItemService.Get(shopItem);
      return result.ToActionResult();
    }

    return Unauthorized();
  }

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

    var (shopItem, getShopItemResult) = _shopCatalogDataProvider.Get(siteId, sku);
    if (!getShopItemResult.IsSuccess || shopItem == null)
      return getShopItemResult.ToActionResult();

    if ((await _authorizationService.AuthorizeAsync(User, new List<ShopDocument> { shopItem }, new ShopAuthorizationRequirement {
      Action = CrudActions.Update
    })).Succeeded) {
      var result = _shopItemService.Update(siteId, sku, requestData);
      return result.ToActionResult();
    }

    return Unauthorized();
  }

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

    var (shopItem, getShopItemResult) = _shopCatalogDataProvider.Get(siteId, sku);
    if (!getShopItemResult.IsSuccess || shopItem == null)
      return getShopItemResult.ToActionResult();

    if ((await _authorizationService.AuthorizeAsync(User, new List<ShopDocument> { shopItem }, new ShopAuthorizationRequirement {
      Action = CrudActions.Delete
    })).Succeeded) {
      var result = _shopCatalogDataProvider.Delete(shopItem.Id);
      return result.ToActionResult();
    }

    return Unauthorized();
  }
}
