using Data.SeaHavenIndustries; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace Api.SeaHavenIndustries.Controllers { [ApiController] [Route("api/dispatch")] public class DispatchPublicController : Controller { private readonly ApplicationDbContext _db; public DispatchPublicController(ApplicationDbContext db) { _db = db; } [HttpGet("accept/{token}")] public async Task Accept(string token) { var dispatch = await _db.Dispatches .Include(d => d.Vendor) .FirstOrDefaultAsync(d => d.AcceptToken == token); if (dispatch == null) { return Content(BuildHtml("Dispatch Not Found", "This dispatch link is invalid or has expired.", "#dc3545"), "text/html"); } if (dispatch.Status != "Sent") { var alreadyMsg = dispatch.AcknowledgedAt.HasValue ? $"This dispatch was already accepted on {dispatch.AcknowledgedAt.Value:MMMM dd, yyyy 'at' h:mm tt} UTC." : "This dispatch has already been processed."; return Content(BuildHtml("Already Accepted", alreadyMsg, "#ffc107"), "text/html"); } dispatch.Status = "Acknowledged"; dispatch.AcknowledgedAt = DateTime.UtcNow; _db.WorkOrderAuditLogs.Add(new WorkOrderAuditLog { WorkOrderId = dispatch.WorkOrderId ?? 0, FieldName = $"Dispatch {dispatch.DispatchNumber} Status", OldValue = "Sent", NewValue = "Acknowledged", Action = "vendor_accept", CreatedAt = DateTime.UtcNow }); await _db.SaveChangesAsync(); var vendorName = dispatch.Vendor?.CompanyName ?? "Vendor"; return Content(BuildHtml( "Dispatch Accepted", $"{vendorName} has accepted dispatch {dispatch.DispatchNumber}.

Thank you for confirming. The dispatcher has been notified.", "#28a745" ), "text/html"); } private static string BuildHtml(string title, string message, string color) { return $@" {title} - Sea Haven Industries
✓

{title}

{message}

"; } } }