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;
/// 
/// 
/// 
[ApiController]
[Route("api/[controller]")]
public class CategoryItemController : ControllerBase {
  private readonly IAuthorizationService _authorizationService;
  private readonly ICategoryItemService _categoryItemService;
  /// 
  /// 
  /// 
  /// 
  /// 
  public CategoryItemController(
    IAuthorizationService authorizationService,
    ICategoryItemService categoryItemService
    ) {
    _authorizationService = authorizationService;
    _categoryItemService = categoryItemService;
  }
  #region Authless methods
  /// 
  /// 
  /// 
  /// 
  /// 
  [HttpGet("{siteId}")]
  public IActionResult GetSlug([FromRoute] Guid siteId, [FromQuery] string slug) {
    var result = _categoryItemService.GetSlug(siteId, slug);
    return result.ToActionResult();
  }
  #endregion
  /// 
  /// 
  /// 
  /// 
  /// 
  /// 
  [HttpPost("{siteId}")]
  public async Task 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();
    }
  }
  /// 
  /// Returns full object
  /// 
  /// 
  /// 
  /// 
  [HttpGet("{siteId}/{categoryId}")]
  public async Task 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();
    }
  }
  /// 
  /// 
  /// 
  /// 
  /// 
  /// 
  /// 
  [HttpPut("{siteId}/{categoryId}")]
  public async Task 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();
  }
  /// 
  /// 
  /// 
  /// 
  /// 
  /// 
  [HttpDelete("{siteId}/{categoryId}")]
  public async Task 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();
  }
}