using Microsoft.AspNetCore.Mvc; using Distribution.Infra.Authorization; using Distribution.Models; namespace Distribution.Features.Invoice; [ApiController] [Route("api/invoices")] public class InvoiceController(InvoiceService service, ICurrentUserService currentUser) : ControllerBase { [HttpGet] public async Task GetAll( [FromQuery] Guid? agentId, [FromQuery] Guid? routeId, [FromQuery] Guid? shopId, [FromQuery] InvoiceStatus? status, [FromQuery] PaymentStatus? paymentStatus, [FromQuery] int page = 1, [FromQuery] int pageSize = 20) { var result = await service.GetAllAsync(agentId, routeId, shopId, status, paymentStatus, page, pageSize); return Ok(result); } [HttpGet("outstanding-summary")] public async Task GetOutstandingSummary() { var result = await service.GetOutstandingSummaryAsync(); return Ok(result); } [HttpGet("{id:guid}")] public async Task GetById(Guid id) { var result = await service.GetByIdAsync(id); return result is null ? NotFound() : Ok(result); } [HttpPost] public async Task Create([FromBody] CreateInvoiceRequest request, CancellationToken ct) { // TEMPORARY: currentUser.UserId is always unset due to the broken auth middleware // (testbug.md #28) — fall back to the explicitly supplied user until that's fixed. var userId = currentUser.UserId ?? request.CreatedByUserId; if (userId is null) return BadRequest(new { error = "CreatedByUserId is required while auth is unavailable" }); try { var result = await service.CreateAsync(request, userId.Value, ct); return CreatedAtAction(nameof(GetById), new { id = result.Id }, result); } catch (KeyNotFoundException) { return NotFound(); } catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); } } [HttpGet("{id:guid}/versions")] public async Task GetVersions(Guid id) { var result = await service.GetVersionsAsync(id); return result is null ? NotFound() : Ok(result); } [HttpPut("{id:guid}/items")] public async Task UpdateItems(Guid id, [FromBody] UpdateInvoiceItemsRequest request, CancellationToken ct) { // TEMPORARY: currentUser.UserId is always unset due to the broken auth middleware // (testbug.md #28) — fall back to the explicitly supplied user until that's fixed. var userId = currentUser.UserId ?? request.EditedByUserId; if (userId is null) return BadRequest(new { error = "EditedByUserId is required while auth is unavailable" }); try { var result = await service.UpdateItemsAsync(id, request, userId.Value, ct); return Ok(result); } catch (KeyNotFoundException) { return NotFound(); } catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); } } [HttpPost("{id:guid}/print")] public async Task Print(Guid id, [FromBody] PrintInvoiceRequest? request, CancellationToken ct) { // TEMPORARY: currentUser.UserId is always unset due to the broken auth middleware // (testbug.md #28) — fall back to the explicitly supplied user until that's fixed. var userId = currentUser.UserId ?? request?.PrintedByUserId; if (userId is null) return BadRequest(new { error = "PrintedByUserId is required while auth is unavailable" }); try { var result = await service.PrintAsync(id, userId.Value, ct); return Ok(result); } catch (KeyNotFoundException) { return NotFound(); } catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); } } [HttpPost("{id:guid}/finalize")] public async Task Finalize(Guid id, [FromBody] FinalizeInvoiceRequest? request, CancellationToken ct) { // TEMPORARY: currentUser.UserId is always unset due to the broken auth middleware // (testbug.md #28) — fall back to the explicitly supplied user until that's fixed. var userId = currentUser.UserId ?? request?.FinalizedByUserId; if (userId is null) return BadRequest(new { error = "FinalizedByUserId is required while auth is unavailable" }); try { var result = await service.FinalizeAsync(id, request ?? new FinalizeInvoiceRequest(), userId.Value, ct); return Ok(result); } catch (KeyNotFoundException) { return NotFound(); } catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); } } }