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

using DomainResults.Mvc;

using WeatherForecast.Services;
using WeatherForecast.Policies;
using Core.Enumerations;
using WeatherForecast.Models.Category.Requests;

namespace WeatherForecast.Controllers;

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

  private readonly IAuthorizationService _authorizationService;
  private readonly ICategoryItemService _categoryItemService;

  /// <summary>
  /// 
  /// </summary>
  /// <param name="authorizationService"></param>
  /// <param name="categoryItemService"></param>
  public CategoryItemController(
    IAuthorizationService authorizationService,
    ICategoryItemService categoryItemService
    ) {
    _authorizationService = authorizationService;
    _categoryItemService = categoryItemService;
  }

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

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

  /// <summary>
  /// Returns full object
  /// </summary>
  /// <param name="siteId"></param>
  /// <param name="categoryId"></param>
  /// <returns></returns>
  [HttpGet("{siteId}/{categoryId}")]
  public async Task<IActionResult> Get([FromRoute] Guid siteId, [FromRoute] Guid categoryId) {
    if ((await _authorizationService.AuthorizeAsync(User, null, new CategoryAuthorizationRequirement {
      Action = CrudActions.Read
    })).Succeeded) {
      var result = _categoryItemService.Get(siteId, categoryId);
      return result.ToActionResult();
    }
    else {
      return Unauthorized();
    }
  }

  /// <summary>
  /// 
  /// </summary>
  /// <param name="siteId"></param>
  /// <param name="categoryId"></param>
  /// <param name="requestData"></param>
  /// <returns></returns>
  [HttpPut("{siteId}/{categoryId}")]
  public async Task<IActionResult> Update([FromRoute] Guid siteId, [FromRoute] Guid categoryId, [FromBody] CategoryItemRequestModel requestData) {
    if ((await _authorizationService.AuthorizeAsync(User, null, new CategoryAuthorizationRequirement {
      Action = CrudActions.Update
    })).Succeeded) {
      var result = _categoryItemService.Update(siteId, categoryId, requestData);
      return result.ToActionResult();
    }
      
    return Unauthorized();
  }

  /// <summary>
  /// 
  /// </summary>
  /// <param name="siteId"></param>
  /// <param name="categoryId"></param>
  /// <returns></returns>
  [HttpDelete("{siteId}/{categoryId}")]
  public async Task<IActionResult> Delete([FromRoute] Guid siteId, [FromRoute] Guid categoryId) {
    if ((await _authorizationService.AuthorizeAsync(User, null, new CategoryAuthorizationRequirement {
      Action = CrudActions.Delete
    })).Succeeded) {
      var result = _categoryItemService.Delete(siteId, categoryId);
      return result.ToActionResult();
    }

    return Unauthorized();
  }
}
