using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SolutionErp.Application.Permissions;
using SolutionErp.Application.Permissions.Dtos;

namespace SolutionErp.Api.Controllers;

[ApiController]
[Route("api/permissions")]
[Authorize(Policy = "Permissions.Read")]
public class PermissionsController(IMediator mediator) : ControllerBase
{
    [HttpGet("by-role/{roleId:guid}")]
    public async Task<ActionResult<List<PermissionDto>>> ListByRole(Guid roleId, CancellationToken ct)
        => Ok(await mediator.Send(new ListPermissionsByRoleQuery(roleId), ct));

    [HttpPut]
    [Authorize(Policy = "Permissions.Update")]
    public async Task<IActionResult> Upsert([FromBody] UpsertPermissionCommand cmd, CancellationToken ct)
    {
        await mediator.Send(cmd, ct);
        return NoContent();
    }
}
