import dotenv from "dotenv";
import path from "path";
dotenv.config({ path: path.resolve(__dirname, ".env") });

import { PrismaClient } from "../generated/prisma";
import { PrismaPg } from "@prisma/adapter-pg";
import bcrypt from "bcrypt";

const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL });
const prisma = new PrismaClient({ adapter } as any);

async function main() {
  const hashedPassword = await bcrypt.hash("123456", 10);

  const admin = await prisma.user.upsert({
    where: { username: "admin" },
    update: {},
    create: { username: "admin", password: hashedPassword },
  });

  console.log("Seed complete: admin user created (username: admin, password: 123456)");

  // Seed 5 default staff members — rename these to real staff names in Settings
  const defaultStaff = ["A", "B", "C", "D", "E"];
  for (const name of defaultStaff) {
    const existing = await prisma.staff.findFirst({
      where: { userId: admin.id, name },
    });
    if (!existing) {
      await prisma.staff.create({
        data: { userId: admin.id, name, role: "Staff" },
      });
    }
  }

  console.log("Seed complete: 5 default staff members created (A, B, C, D, E)");
}

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