using Asp.Versioning; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using SCM.Application.Qualidade; using SCM.Application.Qualidade.Dtos; using SCM.Shared.Api; using SCM.Shared.Constants; namespace SCM.Api.Controllers; /// Módulo MCQ — agentes de qualidade (inspetores/fiscais). [ApiController] [ApiVersion("1.0")] [Route("api/v{version:apiVersion}/agentes")] [Authorize] [Produces("application/json")] public sealed class AgentesController : ControllerBase { private readonly IAgenteService _service; public AgentesController(IAgenteService service) => _service = service; /// Lista todos os agentes (ordenados por nome). [HttpGet] [ProducesResponseType(typeof(IReadOnlyList), StatusCodes.Status200OK)] public async Task>> GetAll(CancellationToken ct = default) => Ok(await _service.GetAllAsync(ct)); /// Obtém um agente pelo identificador. [HttpGet("{id:guid}")] [ProducesResponseType(typeof(AgenteResponse), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)] public async Task> GetById(Guid id, CancellationToken ct = default) => Ok(await _service.GetByIdAsync(id, ct)); /// Cria um agente de qualidade. [HttpPost] [Authorize(Roles = Roles.Administrador + "," + Roles.Gestor)] [ProducesResponseType(typeof(AgenteResponse), StatusCodes.Status201Created)] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status409Conflict)] public async Task> Create( [FromBody] CreateAgenteRequest request, CancellationToken ct = default) { var created = await _service.CreateAsync(request, ct); return CreatedAtAction(nameof(GetById), new { id = created.Id, version = "1.0" }, created); } /// Altera um agente de qualidade. [HttpPut("{id:guid}")] [Authorize(Roles = Roles.Administrador + "," + Roles.Gestor)] [ProducesResponseType(typeof(AgenteResponse), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status409Conflict)] public async Task> Update( Guid id, [FromBody] UpdateAgenteRequest request, CancellationToken ct = default) => Ok(await _service.UpdateAsync(id, request, ct)); /// Exclui um agente (bloqueado se possuir programação associada). [HttpDelete("{id:guid}")] [Authorize(Roles = Roles.Administrador)] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status409Conflict)] public async Task Delete(Guid id, CancellationToken ct = default) { await _service.DeleteAsync(id, ct); return NoContent(); } }