import { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient, Role } from "@prisma/client";
import bcrypt from "bcryptjs";
import {
  branches,
  brands,
  faqs,
  galleryImages,
  inventory,
  offers,
  parts,
  scooters,
  testimonials,
} from "../lib/mock-data";

const connectionString =
  process.env.DATABASE_URL ??
  "postgresql://postgres:postgres@localhost:5432/bashista_auto?schema=public";

const prisma = new PrismaClient({
  adapter: new PrismaPg({ connectionString }),
});

async function main() {
  const passwordHash = await bcrypt.hash("Admin@12345", 12);

  await prisma.user.upsert({
    where: { email: "admin@bashistaauto.com" },
    update: {
      name: "Bashista Auto Admin",
      passwordHash,
      role: Role.SUPER_ADMIN,
    },
    create: {
      name: "Bashista Auto Admin",
      email: "admin@bashistaauto.com",
      passwordHash,
      role: Role.SUPER_ADMIN,
    },
  });

  for (const brand of brands) {
    await prisma.brand.upsert({
      where: { id: brand.id },
      update: brand,
      create: brand,
    });
  }

  for (const branch of branches) {
    await prisma.branch.upsert({
      where: { id: branch.id },
      update: branch,
      create: branch,
    });
  }

  for (const scooter of scooters) {
    const { galleryImages: scooterGalleryImages, model3dGlbUrl, model3dUsdzUrl, ...scooterData } =
      scooter;

    await prisma.scooter.upsert({
      where: { id: scooter.id },
      update: scooterData,
      create: scooterData,
    });

    await prisma.scooterImage.deleteMany({
      where: { scooterId: scooter.id },
    });

    await prisma.scooterImage.createMany({
      data: scooterGalleryImages.map((url, index) => ({
        scooterId: scooter.id,
        url,
        alt: `${scooter.name} gallery image ${index + 1}`,
        sortOrder: index,
      })),
    });

    await prisma.scooterAsset3D.upsert({
      where: { scooterId: scooter.id },
      update: {
        glbUrl: model3dGlbUrl,
        usdzUrl: model3dUsdzUrl,
        status: model3dGlbUrl ? "PUBLISHED" : "PENDING",
      },
      create: {
        scooterId: scooter.id,
        glbUrl: model3dGlbUrl,
        usdzUrl: model3dUsdzUrl,
        status: model3dGlbUrl ? "PUBLISHED" : "PENDING",
      },
    });
  }

  for (const item of inventory) {
    await prisma.inventory.upsert({
      where: {
        scooterId_branchId: {
          scooterId: item.scooterId,
          branchId: item.branchId,
        },
      },
      update: {
        id: item.id,
        status: item.status,
        quantity: item.quantity,
        testRideAvailable: item.testRideAvailable,
      },
      create: item,
    });
  }

  for (const part of parts) {
    await prisma.part.upsert({
      where: { id: part.id },
      update: part,
      create: part,
    });
  }

  for (const offer of offers) {
    await prisma.offer.upsert({
      where: { id: offer.id },
      update: {
        ...offer,
        validUntil: new Date(`${offer.validUntil}T00:00:00.000Z`),
      },
      create: {
        ...offer,
        validUntil: new Date(`${offer.validUntil}T00:00:00.000Z`),
      },
    });
  }

  for (const image of galleryImages) {
    await prisma.galleryImage.upsert({
      where: { id: image.id },
      update: image,
      create: image,
    });
  }

  for (const testimonial of testimonials) {
    await prisma.testimonial.upsert({
      where: { id: testimonial.id },
      update: testimonial,
      create: testimonial,
    });
  }

  for (const faq of faqs) {
    await prisma.faq.upsert({
      where: { id: faq.id },
      update: faq,
      create: faq,
    });
  }

  console.log("Seed complete: admin, showroom content, products, inventory and offers.");
}

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