import { StripeSync } from "@supabase/stripe-sync-engine";
import type { AppConfig } from "../config.js";

export const createStripeSync = (config: AppConfig): StripeSync => {
  const ssl = {
    ca: Buffer.from(config.SUPABASE_CA_CERT, "base64").toString("utf-8"),
  };

  return new StripeSync({
    stripeSecretKey: config.STRIPE_SECRET_KEY || "",
    stripeWebhookSecret: config.STRIPE_WEBHOOK_SECRET || "",
    schema: "stripe", // Use stripe schema (library default)
    poolConfig: {
      connectionString: config.SUPABASE_CONNECTION_STRING || "", // Direct Postgres connection string
      ssl,
      max: 10,
    },
    // Optional: force refetch from Stripe API to avoid stale data
    revalidateObjectsViaStripeApi: ["subscription", "customer"],
  });
};
