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

namespace ROLAC.API.Controllers;

[ApiController]
[Route("api/ministries")]
[Authorize]
public class MinistriesController : ControllerBase
{
    private readonly IMinistryService _svc;
    public MinistriesController(IMinistryService svc) => _svc = svc;

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

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

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

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