import type { ActionFunctionArgs } from "react-router";
import { authenticate } from "../shopify.server";
import db from "../db.server";
import {
  generateAndEmailInvoice,
  isWireTransferOrder,
} from "../services/invoice/automations.server";

/**
 * orders/fulfilled — Automation 2: when an order is fulfilled and is NOT a
 * wire-transfer order (e.g. paid by card), automatically email the invoice
 * to the customer. Wire-transfer orders are intentionally skipped here
 * because Automation 1 already emailed them at order-create time.
 */
export const action = async ({ request }: ActionFunctionArgs) => {
  const { shop, topic, payload, session, admin } = await authenticate.webhook(request);
  console.log(`Received ${topic} webhook for ${shop}`);

  if (!session || !admin) {
    // App was uninstalled before the webhook drained — nothing to do.
    return new Response();
  }

  const settings = await db.shopSettings.findUnique({ where: { shopDomain: shop } });
  if (!settings?.autoEmailOnFulfilledNonWireTransfer) return new Response();

  const orderId = payload?.id;
  if (orderId == null) return new Response();

  const gateways: string[] = Array.isArray(payload?.payment_gateway_names)
    ? payload.payment_gateway_names
    : [];
  if (isWireTransferOrder(gateways, settings.wireTransferGatewayNames)) {
    // Wire-transfer order — handled by Automation 1, skip here.
    return new Response();
  }

  try {
    const result = await generateAndEmailInvoice({
      shopDomain: shop,
      admin,
      orderId,
      customerLocale: typeof payload?.customer_locale === "string" ? payload.customer_locale : undefined,
    });
    if (!result.ok) {
      console.warn(`auto-email (fulfilled) failed for order ${orderId} on ${shop}: ${result.reason}`);
    }
  } catch (err) {
    console.error(`auto-email (fulfilled) crashed for order ${orderId} on ${shop}:`, err);
  }

  return new Response();
};
