import bcrypt from "bcryptjs";
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

async function main() {
  const adminEmail = "admin@ywyw.com";
  const customerEmail = "cust@ywyw.com";

  const passwordHash = await bcrypt.hash("Dev1234#", 10);

  const admin = await prisma.user.upsert({
    where: { email: adminEmail },
    update: {},
    create: {
      email: adminEmail,
      name: "Admin User",
      role: "ADMIN",
      firstName: "Admin",
      lastName: "User",
      gender: "OTHER",
      emailVerified: true,
      forcePasswordReset: false,
      isActive: true,
    },
  });

  await prisma.credential.upsert({
    where: { userId: admin.id },
    update: { password: passwordHash },
    create: { userId: admin.id, password: passwordHash },
  });

  console.log("✅ Admin user created:", adminEmail);

  const customer = await prisma.user.upsert({
    where: { email: customerEmail },
    update: {},
    create: {
      email: customerEmail,
      name: "Customer User",
      role: "USER",
      firstName: "Customer",
      lastName: "User",
      gender: "OTHER",
      emailVerified: true,
      forcePasswordReset: false,
      isActive: true,
    },
  });

  await prisma.credential.upsert({
    where: { userId: customer.id },
    update: { password: passwordHash },
    create: { userId: customer.id, password: passwordHash },
  });

  console.log("✅ Customer user created:", customerEmail);

  const existingWebinars = await prisma.webinar.count();
  if (existingWebinars === 0) {
    const now = Date.now();
    const sample = [
      {
        title: "Estate Planning Fundamentals",
        description: "Learn the foundations of estate planning, wills, and trusts.",
        speaker: "Emily Roberts",
        startAt: new Date(now + 7 * 86400000),
        duration: 90,
        bannerUrl: null,
        category: "Basics",
        visibility: "PUBLIC",
        isActive: true,
        capacity: 50,
        priceCents: 0,
        learningPoints: [
          "Understanding the basics of wills and trusts",
          "Key differences between revocable and irrevocable trusts",
          "Common estate planning mistakes to avoid",
          "When to update your estate plan",
        ],
        meetingInfo: {},
      },
      {
        title: "Avoiding Probate: Strategies & Solutions",
        description: "Practical strategies to reduce or avoid probate delays.",
        speaker: "David Martinez",
        startAt: new Date(now + 10 * 86400000),
        duration: 75,
        bannerUrl: null,
        category: "Planning",
        visibility: "PUBLIC",
        isActive: true,
        capacity: 60,
        priceCents: 0,
        learningPoints: [
          "How probate works and why it can be costly",
          "Living trusts as probate avoidance tools",
          "Joint ownership strategies",
          "Beneficiary designations and their importance",
        ],
        meetingInfo: {},
      },
      {
        title: "Tax-Efficient Estate Planning",
        description: "Minimize taxes and preserve wealth across generations.",
        speaker: "Jennifer Thompson",
        startAt: new Date(now + 14 * 86400000),
        duration: 90,
        bannerUrl: null,
        category: "Tax",
        visibility: "PUBLIC",
        isActive: true,
        capacity: 40,
        priceCents: 4900,
        learningPoints: [
          "Current federal and state estate tax exemptions",
          "Gift tax strategies and annual exclusions",
          "Charitable giving techniques for tax benefits",
          "Generation-skipping transfer tax planning",
        ],
        meetingInfo: {},
      },
      {
        title: "Healthcare Directives & Powers of Attorney",
        description: "Understand advanced directives and medical decision-making.",
        speaker: "Lisa Patterson",
        startAt: new Date(now + 17 * 86400000),
        duration: 60,
        bannerUrl: null,
        category: "Healthcare",
        visibility: "PUBLIC",
        isActive: true,
        capacity: 80,
        priceCents: 0,
        learningPoints: [
          "Types of healthcare directives and their purposes",
          "Choosing the right healthcare proxy",
          "Living wills vs. healthcare powers of attorney",
          "HIPAA authorizations and medical records access",
        ],
        meetingInfo: {},
      },
      {
        title: "Family Wealth Transfer (Private Session)",
        description: "Invite-only workshop for complex family asset structures.",
        speaker: "Michael Chen",
        startAt: new Date(now + 21 * 86400000),
        duration: 120,
        bannerUrl: null,
        category: "Advanced",
        visibility: "PRIVATE",
        isActive: true,
        capacity: 20,
        priceCents: 9900,
        learningPoints: [
          "Advanced trust structures for wealth preservation",
          "Family limited partnerships and LLCs",
          "Succession planning for family businesses",
          "Coordinating estate plans across multiple jurisdictions",
        ],
        meetingInfo: {},
      },
    ];

    await prisma.webinar.createMany({ data: sample as any });
    console.log("✅ Sample webinars created");
  }

  const appSetup = await prisma.appSetup.findUnique({ where: { id: 1 } });
  if (!appSetup) {
    await prisma.appSetup.create({
      data: {
        id: 1,
        googleAuthEnabled: false,
        socials: {},
        categories: ["Basics", "Planning", "Tax", "Healthcare", "Advanced"],
      },
    });
    console.log("✅ App setup row created");
  }
}

main()
  .catch((e) => {
    console.error(e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });
