import { PrismaClient, ProjectTemperature, UserRole } from "@prisma/client";
import { hashPassword } from "../lib/password";

const prisma = new PrismaClient();

async function main() {
  const poleNames = [
    "Strategie a impact",
    "Innovation et experimentation",
    "UX/UI responsable",
    "Design des vulnerabilites"
  ];

  const poles = await Promise.all(
    poleNames.map((name) =>
      prisma.pole.upsert({
        where: { name },
        update: {},
        create: { name }
      })
    )
  );

  const admin = await prisma.user.upsert({
    where: { email: "admin@sensipode.local" },
    update: {},
    create: {
      email: "admin@sensipode.local",
      passwordHash: await hashPassword("Sensipode-SOS-2026!"),
      role: UserRole.ADMIN
    }
  });

  const claraUser = await prisma.user.upsert({
    where: { email: "clara@sensipode.local" },
    update: {},
    create: {
      email: "clara@sensipode.local",
      passwordHash: await hashPassword("Sensipode-Clara-2026!"),
      role: UserRole.PROJECT_MANAGER
    }
  });

  const clara = await prisma.collaborator.upsert({
    where: { email: "clara@sensipode.local" },
    update: {},
    create: {
      userId: claraUser.id,
      firstName: "Clara",
      lastName: "Martin",
      email: "clara@sensipode.local",
      jobTitle: "Cheffe de projet",
      weeklyHours: 37,
      weeklyCapacityDays: 5,
      primaryPoleId: poles[0].id
    }
  });

  const client = await prisma.client.upsert({
    where: { name: "Ville partenaire" },
    update: {},
    create: {
      name: "Ville partenaire",
      billingEmail: "facturation@ville-partenaire.example"
    }
  });

  await prisma.project.upsert({
    where: { id: "seed-refonte-parcours-usagers" },
    update: {},
    create: {
      id: "seed-refonte-parcours-usagers",
      clientId: client.id,
      type: "CLIENT",
      name: "Refonte parcours usagers",
      budgetCents: 8400000,
      managerId: clara.id,
      startsAt: new Date("2026-09-01"),
      endsAt: new Date("2027-03-31"),
      status: "ACTIVE",
      temperature: ProjectTemperature.ORANGE,
      progressPercent: 62
    }
  });
}

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