import type { ActionFunctionArgs } from "@remix-run/node";
import { authenticate } from "../shopify.server";
import db from "../db.server";

export const action = async ({ request }: ActionFunctionArgs) => {
  const { shop, topic } = await authenticate.webhook(request);
  console.log(`Received ${topic} webhook for ${shop}`);

  // Delete all shop data (GDPR compliance)
  try {
    await db.shopRule.deleteMany({ where: { shopId: shop } });
    await db.shop.delete({ where: { id: shop } });
  } catch {
    // Shop may not exist
  }

  return new Response();
};
