using Distribution.Infra.Authorization; using Distribution.Models; using Microsoft.AspNetCore.Mvc; namespace Distribution.Features.ShopPO; [ApiController] [Route("api/shop-pos")] public class ShopPOController(ShopPOService service, ICurrentUserService currentUser) : ControllerBase { [HttpGet] public async Task GetAll( [FromQuery] Guid? agentId, [FromQuery] Guid? routeId, [FromQuery] Guid? regionId, [FromQuery] Guid? shopId, [FromQuery] bool? confirmed, [FromQuery] ShopPOStatus? status, [FromQuery] int page = 1, [FromQuery] int pageSize = 20) { var result = await service.GetAllAsync(agentId, routeId, regionId, shopId, confirmed, status, page, pageSize); 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); } [HttpGet("confirmed-without-invoice")] public async Task GetConfirmedWithoutInvoice( [FromQuery] Guid? agentId, [FromQuery] Guid? routeId, [FromQuery] Guid? shopId) { var result = await service.GetConfirmedWithoutInvoiceAsync(agentId, routeId, shopId); return Ok(result); } [HttpPost] public async Task Create(CreateShopPORequest 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.RaisedByUserId; if (userId is null) return BadRequest(new { error = "RaisedByUserId 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 (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); } } [HttpPut("{id:guid}/items")] public async Task UpdateItems(Guid id, UpdateShopPOItemsRequest request, CancellationToken ct) { try { var result = await service.UpdateItemsAsync(id, request, ct); return Ok(result); } catch (KeyNotFoundException) { return NotFound(); } catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); } } [HttpPost("{id:guid}/confirm")] public async Task Confirm(Guid id, ConfirmShopPORequest 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.ConfirmedByUserId; if (userId is null) return BadRequest(new { error = "ConfirmedByUserId is required while auth is unavailable" }); try { var result = await service.ConfirmAsync(id, userId.Value, ct); return Ok(result); } catch (KeyNotFoundException) { return NotFound(); } catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); } } [HttpPost("{id:guid}/confirm-and-create-invoice")] public async Task ConfirmAndCreateInvoice(Guid id, ConfirmShopPORequest 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.ConfirmedByUserId; if (userId is null) return BadRequest(new { error = "ConfirmedByUserId is required while auth is unavailable" }); try { var result = await service.ConfirmAndCreateInvoiceAsync(id, userId.Value, ct); return Ok(result); } catch (KeyNotFoundException) { return NotFound(); } catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); } } [HttpPost("{id:guid}/cancel")] public async Task Cancel(Guid id, CancelShopPORequest 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.CancelledByUserId; if (userId is null) return BadRequest(new { error = "CancelledByUserId is required while auth is unavailable" }); try { var result = await service.CancelAsync(id, userId.Value, request.Reason, ct); return Ok(result); } catch (KeyNotFoundException) { return NotFound(); } catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); } } }