import "dotenv/config";
import bcrypt from "bcryptjs";
import { PrismaClient } from "../src/generated/prisma/client";
import { PrismaMariaDb } from "@prisma/adapter-mariadb";

const adapter = new PrismaMariaDb(
  process.env.DATABASE_URL ?? "mysql://root@localhost:3306/salon_site_dev"
);
const prisma = new PrismaClient({ adapter });

async function main() {
  const adminEmail = process.env.ADMIN_EMAIL;
  const adminPassword = process.env.ADMIN_PASSWORD;

  if (!adminEmail || !adminPassword) {
    throw new Error("Set ADMIN_EMAIL and ADMIN_PASSWORD in .env before seeding");
  }

  const passwordHash = await bcrypt.hash(adminPassword, 10);
  await prisma.adminUser.upsert({
    where: { email: adminEmail },
    update: { passwordHash },
    create: { email: adminEmail, passwordHash },
  });
  console.log(`Admin user ready: ${adminEmail}`);

  await prisma.settings.upsert({
    where: { id: "singleton" },
    update: {},
    create: { id: "singleton" },
  });

  const services = [
    {
      name: "Signature Haircut",
      description: "Precision cut and style tailored to you, finished with a blowout.",
      durationMinutes: 60,
      priceCents: 6500,
    },
    {
      name: "Full Color",
      description: "All-over color application with gloss and blowout finish.",
      durationMinutes: 120,
      priceCents: 15000,
    },
    {
      name: "Balayage",
      description: "Hand-painted highlights for a soft, sun-kissed dimension.",
      durationMinutes: 180,
      priceCents: 22000,
    },
    {
      name: "Blowout & Style",
      description: "Wash, blowout, and finishing style for any occasion.",
      durationMinutes: 45,
      priceCents: 5000,
    },
  ];

  for (const service of services) {
    const existing = await prisma.service.findFirst({ where: { name: service.name } });
    if (!existing) {
      await prisma.service.create({ data: service });
    }
  }
  console.log(`Seeded ${services.length} services (skipping any that already exist).`);
}

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