using Asp.Versioning;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SCM.Application.Controle;
using SCM.Application.Controle.Dtos;
using SCM.Shared.Api;
using SCM.Shared.Constants;
namespace SCM.Api.Controllers;
/// Módulo MBC — itens de controle.
[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/itens-controle")]
[Authorize]
[Produces("application/json")]
public sealed class ItensControleController : ControllerBase
{
private readonly IItemControleService _service;
public ItensControleController(IItemControleService service) => _service = service;
/// Lista os itens de controle de uma obra (ordenados por código).
[HttpGet]
[ProducesResponseType(typeof(IReadOnlyList), StatusCodes.Status200OK)]
public async Task>> GetByObra(
[FromQuery] Guid obraId, CancellationToken ct = default)
=> Ok(await _service.GetByObraAsync(obraId, ct));
/// Obtém um item de controle pelo identificador.
[HttpGet("{id:guid}")]
[ProducesResponseType(typeof(ItemControleResponse), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)]
public async Task> GetById(Guid id, CancellationToken ct = default)
=> Ok(await _service.GetByIdAsync(id, ct));
/// Cria um item de controle.
[HttpPost]
[Authorize(Roles = Roles.Administrador + "," + Roles.Gestor)]
[ProducesResponseType(typeof(ItemControleResponse), StatusCodes.Status201Created)]
[ProducesResponseType(typeof(ApiResponse), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ApiResponse), StatusCodes.Status409Conflict)]
public async Task> Create(
[FromBody] CreateItemControleRequest 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 item de controle.
[HttpPut("{id:guid}")]
[Authorize(Roles = Roles.Administrador + "," + Roles.Gestor)]
[ProducesResponseType(typeof(ItemControleResponse), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ApiResponse), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ApiResponse), StatusCodes.Status409Conflict)]
public async Task> Update(
Guid id, [FromBody] UpdateItemControleRequest request, CancellationToken ct = default)
=> Ok(await _service.UpdateAsync(id, request, ct));
/// Exclui um item de controle (bloqueado se possuir itens programados).
[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();
}
}