using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ROLAC.API.Authorization;
using ROLAC.API.DTOs.Giving;
using ROLAC.API.Services;

namespace ROLAC.API.Controllers;

[ApiController]
[Route("api/giving-categories")]
[Authorize]
public class GivingCategoriesController : ControllerBase
{
    private readonly IGivingCategoryService _svc;
    public GivingCategoriesController(IGivingCategoryService svc) => _svc = svc;

    [HttpGet]
    [HasPermission(Modules.GivingCategories, PermissionActions.Read)]
    public async Task<IActionResult> GetAll([FromQuery] bool includeInactive = false)
        => Ok(await _svc.GetAllAsync(includeInactive));

    [HttpPost]
    [HasPermission(Modules.GivingCategories, PermissionActions.Write)]
    public async Task<IActionResult> Create([FromBody] CreateGivingCategoryRequest request)
    {
        var id = await _svc.CreateAsync(request);
        return CreatedAtAction(nameof(GetAll), new { id }, new { id });
    }

    [HttpPut("{id:int}")]
    [HasPermission(Modules.GivingCategories, PermissionActions.Write)]
    public async Task<IActionResult> Update(int id, [FromBody] UpdateGivingCategoryRequest request)
    {
        try   { await _svc.UpdateAsync(id, request); return NoContent(); }
        catch (KeyNotFoundException) { return NotFound(); }
    }

    [HttpDelete("{id:int}")]
    [HasPermission(Modules.GivingCategories, PermissionActions.Delete)]
    public async Task<IActionResult> Deactivate(int id)
    {
        try   { await _svc.DeactivateAsync(id); return NoContent(); }
        catch (KeyNotFoundException) { return NotFound(); }
    }
}
