using Asp.Versioning;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SCM.Application.Materiais;
using SCM.Application.Materiais.Dtos;
using SCM.Shared.Api;
using SCM.Shared.Constants;
namespace SCM.Api.Controllers;
/// Delivery Orders (MSP) — recebimento físico de itens de material, por obra.
[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/delivery-orders")]
[Authorize]
[Produces("application/json")]
public sealed class DeliveryOrdersController : ControllerBase
{
private readonly IDeliveryOrderService _service;
public DeliveryOrdersController(IDeliveryOrderService service) => _service = service;
/// Lista as DOs de uma obra (filtros opcionais: notaFiscal LIKE, tipo macro).
[HttpGet]
[ProducesResponseType(typeof(IReadOnlyList), StatusCodes.Status200OK)]
public async Task>> GetByObra(
[FromQuery] Guid obraId, [FromQuery] string? notaFiscal = null,
[FromQuery] Guid? tipoMaterialMacroId = null, CancellationToken ct = default)
=> Ok(await _service.GetByObraAsync(obraId, notaFiscal, tipoMaterialMacroId, ct));
/// Obtém uma DO pelo identificador.
[HttpGet("{id:guid}")]
[ProducesResponseType(typeof(DeliveryOrderResponse), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)]
public async Task> GetById(Guid id, CancellationToken ct = default)
=> Ok(await _service.GetByIdAsync(id, ct));
/// Cria uma DO (par nota fiscal + item único por obra; tipo macro precisa de agrupador).
[HttpPost]
[Authorize(Roles = Roles.Administrador + "," + Roles.Gestor + "," + Roles.Engenheiro)]
[ProducesResponseType(typeof(DeliveryOrderResponse), StatusCodes.Status201Created)]
[ProducesResponseType(typeof(ApiResponse), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ApiResponse), StatusCodes.Status409Conflict)]
public async Task> Create(
[FromBody] CreateDeliveryOrderRequest request, CancellationToken ct = default)
{
var created = await _service.CreateAsync(request, ct);
return CreatedAtAction(nameof(GetById), new { id = created.Id, version = "1.0" }, created);
}
/// Altera uma DO (a obra é imutável).
[HttpPut("{id:guid}")]
[Authorize(Roles = Roles.Administrador + "," + Roles.Gestor + "," + Roles.Engenheiro)]
[ProducesResponseType(typeof(DeliveryOrderResponse), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ApiResponse), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ApiResponse), StatusCodes.Status409Conflict)]
public async Task> Update(
Guid id, [FromBody] UpdateDeliveryOrderRequest request, CancellationToken ct = default)
=> Ok(await _service.UpdateAsync(id, request, ct));
/// Exclui uma DO.
[HttpDelete("{id:guid}")]
[Authorize(Roles = Roles.Administrador + "," + Roles.Gestor)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)]
public async Task Delete(Guid id, CancellationToken ct = default)
{
await _service.DeleteAsync(id, ct);
return NoContent();
}
}