import type { ActionFunctionArgs } from "react-router";
import { authenticate } from "../shopify.server";
import { generateInvoice } from "../services/invoice/generateInvoice.server";

/**
 * Flow action endpoint: "Generate invoice for order".
 *
 * Expected payload (verified by `authenticate.flow`):
 *   {
 *     "shop_id": "...",
 *     "shopify_domain": "...",
 *     "properties": { "order_id": "gid://shopify/Order/..." },
 *     ...
 *   }
 *
 * Returns 200 on success (Flow treats any 2xx as success) and 4xx/5xx
 * with a JSON body describing the failure.
 */
export const action = async ({ request }: ActionFunctionArgs) => {
  const { session, admin, payload } = await authenticate.flow(request);

  const orderId = extractOrderId(payload);
  if (!orderId) {
    return Response.json(
      { ok: false, error: "Missing 'order_id' in Flow payload properties." },
      { status: 400 },
    );
  }

  try {
    const result = await generateInvoice({
      shopDomain: session.shop,
      admin,
      orderId,
    });
    return { ok: true, ...result };
  } catch (err) {
    const message = err instanceof Error ? err.message : String(err);
    console.error("flow.generate-invoice failed:", err);
    return Response.json({ ok: false, error: message }, { status: 422 });
  }
};

function extractOrderId(payload: unknown): string | null {
  if (!payload || typeof payload !== "object") return null;
  const props = (payload as { properties?: Record<string, unknown> }).properties;
  if (!props || typeof props !== "object") return null;
  const raw = props["order_id"];
  if (typeof raw === "string" && raw.length > 0) return raw;
  if (typeof raw === "number") return String(raw);
  return null;
}
