import { BillingInterval, PrismaClient, SubscriptionStatus } from "@prisma/client";
import { plans } from "./plan-catalog";

const db = new PrismaClient();

async function main() {
  for (const plan of plans) {
    await db.plan.upsert({ where: { code: plan.code }, update: plan, create: plan });
  }

  const customer = await db.user.upsert({
    where: { email: "demo@aiwebcommand.local" },
    update: { name: "Demo Woo Customer", wooCustomerId: "demo-woo-customer-1001" },
    create: { email: "demo@aiwebcommand.local", name: "Demo Woo Customer", wooCustomerId: "demo-woo-customer-1001" },
  });
  const organization = await db.organization.upsert({
    where: { slug: "demo-agency-workspace" },
    update: { name: "Northstar Digital", billingContactEmail: customer.email, billingContactName: customer.name },
    create: { name: "Northstar Digital", slug: "demo-agency-workspace", billingContactEmail: customer.email, billingContactName: customer.name },
  });
  await db.organizationMember.upsert({
    where: { organizationId_userId: { organizationId: organization.id, userId: customer.id } },
    update: { role: "OWNER" },
    create: { organizationId: organization.id, userId: customer.id, role: "OWNER" },
  });
  const plan = await db.plan.findUniqueOrThrow({ where: { code: "growth-10" } });
  await db.subscription.upsert({
    where: { organizationId: organization.id },
    update: { planId: plan.id, status: SubscriptionStatus.ACTIVE, billingInterval: BillingInterval.ANNUAL, provider: "woocommerce", providerCustomerId: customer.wooCustomerId, providerSubscriptionId: "demo-subscription-1001", currentPeriodEnd: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000) },
    create: { organizationId: organization.id, planId: plan.id, status: SubscriptionStatus.ACTIVE, billingInterval: BillingInterval.ANNUAL, provider: "woocommerce", providerCustomerId: customer.wooCustomerId, providerSubscriptionId: "demo-subscription-1001", currentPeriodEnd: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000) },
  });
  await db.plan.deleteMany({ where: { code: { in: ["individual-1", "agency-10"] }, subscriptions: { none: {} } } });
  console.log("Demo Woo customer ready: demo@aiwebcommand.local (Growth annual, active).");
}

main().finally(() => db.$disconnect());
