import { NextResponse } from "next/server";
import { getAuthFromRequest } from "@open-mercato/shared/lib/auth/server";
import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { readJsonSafe } from "@open-mercato/shared/lib/http/readJsonSafe";
import { shippingCarrierUpstreamErrorResponse } from "../../../lib/upstream-error-response.js";
import { trackingRefreshSchema } from "../../../data/validators.js";
import { shippingCarriersTag } from "../../openapi.js";
const metadata = {
  path: "/shipping-carriers/tracking/refresh",
  POST: { requireAuth: true, requireFeatures: ["shipping_carriers.manage"] }
};
async function POST(req) {
  const auth = await getAuthFromRequest(req);
  if (!auth?.tenantId || !auth.orgId) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }
  const payload = await readJsonSafe(req);
  const parsed = trackingRefreshSchema.safeParse(payload);
  if (!parsed.success) {
    return NextResponse.json({ error: "Invalid payload", details: parsed.error.flatten() }, { status: 422 });
  }
  const container = await createRequestContainer();
  const service = container.resolve("shippingCarrierService");
  try {
    const tracking = await service.refreshTracking({
      ...parsed.data,
      organizationId: auth.orgId,
      tenantId: auth.tenantId
    });
    return NextResponse.json(tracking);
  } catch (error) {
    return shippingCarrierUpstreamErrorResponse("tracking.refresh", error);
  }
}
const openApi = {
  tags: [shippingCarriersTag],
  summary: "Refresh tracking",
  methods: {
    POST: {
      summary: "Refresh tracking and persist the latest shipment status",
      tags: [shippingCarriersTag],
      responses: [
        { status: 200, description: "Tracking refreshed and shipment status persisted" },
        { status: 422, description: "Validation failed" },
        { status: 502, description: "Provider upstream error" }
      ]
    }
  }
};
export {
  POST,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
