import { PrismaClient } from '@prisma/client';
import { hash } from 'bcryptjs';

const prisma = new PrismaClient();

async function main() {
  console.log('Seeding database...');

  const adminPassword = await hash('admin123', 12);
  const driverPassword = await hash('driver123', 12);

  const admin = await prisma.user.upsert({
    where: { email: 'admin@savethemoment.de' },
    update: {},
    create: {
      email: 'admin@savethemoment.de',
      name: 'Dennis Forte',
      password: adminPassword,
      role: 'ADMIN',
      phoneNumber: '+49 123 456789',
    },
  });

  const driver1 = await prisma.user.upsert({
    where: { email: 'fahrer1@savethemoment.de' },
    update: {},
    create: {
      email: 'fahrer1@savethemoment.de',
      name: 'Max Mustermann',
      password: driverPassword,
      role: 'DRIVER',
      phoneNumber: '+49 123 456780',
    },
  });

  const driver2 = await prisma.user.upsert({
    where: { email: 'fahrer2@savethemoment.de' },
    update: {},
    create: {
      email: 'fahrer2@savethemoment.de',
      name: 'Anna Schmidt',
      password: driverPassword,
      role: 'DRIVER',
      phoneNumber: '+49 123 456781',
    },
  });

  const luebeck = await prisma.location.upsert({
    where: { slug: 'luebeck' },
    update: {},
    create: {
      name: 'Lübeck',
      city: 'Lübeck',
      slug: 'luebeck',
      websiteUrl: 'https://fotobox-luebeck.de',
      contactEmail: 'info@fotobox-luebeck.de',
      warehouseAddress: 'Wahmstraße 83',
      warehouseZip: '23552',
      warehouseCity: 'Lübeck',
    },
  });

  const hamburg = await prisma.location.upsert({
    where: { slug: 'hamburg' },
    update: {},
    create: {
      name: 'Hamburg',
      city: 'Hamburg',
      slug: 'hamburg',
      websiteUrl: 'https://hamburg-fotobox.de',
      contactEmail: 'info@hamburg-fotobox.de',
      warehouseAddress: 'Wahmstraße 83',
      warehouseZip: '23552',
      warehouseCity: 'Lübeck',
    },
  });

  const kiel = await prisma.location.upsert({
    where: { slug: 'kiel' },
    update: {},
    create: {
      name: 'Kiel',
      city: 'Kiel',
      slug: 'kiel',
      websiteUrl: 'https://fotobox-kiel.de',
      contactEmail: 'info@fotobox-kiel.de',
      warehouseAddress: 'Wahmstraße 83',
      warehouseZip: '23552',
      warehouseCity: 'Lübeck',
    },
  });

  const berlin = await prisma.location.upsert({
    where: { slug: 'berlin' },
    update: {},
    create: {
      name: 'Berlin',
      city: 'Berlin',
      slug: 'berlin',
      websiteUrl: 'https://fotobox-potsdam.de',
      contactEmail: 'info@fotobox-potsdam.de',
      warehouseAddress: 'Wahmstraße 83',
      warehouseZip: '23552',
      warehouseCity: 'Lübeck',
    },
  });

  const rostock = await prisma.location.upsert({
    where: { slug: 'rostock' },
    update: {},
    create: {
      name: 'Rostock',
      city: 'Rostock',
      slug: 'rostock',
      websiteUrl: 'https://fotobox-rostock.de',
      contactEmail: 'info@fotobox-rostock.de',
      warehouseAddress: 'Wahmstraße 83',
      warehouseZip: '23552',
      warehouseCity: 'Lübeck',
    },
  });

  await prisma.priceConfig.createMany({
    data: [
      { locationId: luebeck.id, model: 'VINTAGE_SMILE', basePrice: 399, kmFlatRate: 60, kmFlatRateUpTo: 15, pricePerKm: 0.40, kmMultiplier: 4 },
      { locationId: luebeck.id, model: 'VINTAGE_PHOTOS', basePrice: 449, kmFlatRate: 60, kmFlatRateUpTo: 15, pricePerKm: 0.40, kmMultiplier: 4 },
      { locationId: luebeck.id, model: 'NOSTALGIE', basePrice: 499, kmFlatRate: 60, kmFlatRateUpTo: 15, pricePerKm: 0.40, kmMultiplier: 4 },
      { locationId: luebeck.id, model: 'MAGIC_MIRROR', basePrice: 599, kmFlatRate: 60, kmFlatRateUpTo: 15, pricePerKm: 0.40, kmMultiplier: 4 },
      
      { locationId: hamburg.id, model: 'VINTAGE_SMILE', basePrice: 419, kmFlatRate: 100, kmFlatRateUpTo: 60, pricePerKm: 0.40, kmMultiplier: 4 },
      { locationId: hamburg.id, model: 'VINTAGE_PHOTOS', basePrice: 469, kmFlatRate: 100, kmFlatRateUpTo: 60, pricePerKm: 0.40, kmMultiplier: 4 },
      { locationId: hamburg.id, model: 'NOSTALGIE', basePrice: 519, kmFlatRate: 100, kmFlatRateUpTo: 60, pricePerKm: 0.40, kmMultiplier: 4 },
      { locationId: hamburg.id, model: 'MAGIC_MIRROR', basePrice: 619, kmFlatRate: 100, kmFlatRateUpTo: 60, pricePerKm: 0.40, kmMultiplier: 4 },
      
      { locationId: kiel.id, model: 'VINTAGE_SMILE', basePrice: 389, kmFlatRate: 60, kmFlatRateUpTo: 15, pricePerKm: 0.40, kmMultiplier: 4 },
      { locationId: kiel.id, model: 'VINTAGE_PHOTOS', basePrice: 439, kmFlatRate: 60, kmFlatRateUpTo: 15, pricePerKm: 0.40, kmMultiplier: 4 },
      { locationId: kiel.id, model: 'NOSTALGIE', basePrice: 489, kmFlatRate: 60, kmFlatRateUpTo: 15, pricePerKm: 0.40, kmMultiplier: 4 },
      { locationId: kiel.id, model: 'MAGIC_MIRROR', basePrice: 589, kmFlatRate: 60, kmFlatRateUpTo: 15, pricePerKm: 0.40, kmMultiplier: 4 },
      
      { locationId: berlin.id, model: 'VINTAGE_SMILE', basePrice: 409, kmFlatRate: 60, kmFlatRateUpTo: 15, pricePerKm: 0.40, kmMultiplier: 4 },
      { locationId: berlin.id, model: 'VINTAGE_PHOTOS', basePrice: 459, kmFlatRate: 60, kmFlatRateUpTo: 15, pricePerKm: 0.40, kmMultiplier: 4 },
      { locationId: berlin.id, model: 'NOSTALGIE', basePrice: 509, kmFlatRate: 60, kmFlatRateUpTo: 15, pricePerKm: 0.40, kmMultiplier: 4 },
      { locationId: berlin.id, model: 'MAGIC_MIRROR', basePrice: 609, kmFlatRate: 60, kmFlatRateUpTo: 15, pricePerKm: 0.40, kmMultiplier: 4 },
      
      { locationId: rostock.id, model: 'VINTAGE_SMILE', basePrice: 379, kmFlatRate: 60, kmFlatRateUpTo: 15, pricePerKm: 0.40, kmMultiplier: 4 },
      { locationId: rostock.id, model: 'VINTAGE_PHOTOS', basePrice: 429, kmFlatRate: 60, kmFlatRateUpTo: 15, pricePerKm: 0.40, kmMultiplier: 4 },
      { locationId: rostock.id, model: 'NOSTALGIE', basePrice: 479, kmFlatRate: 60, kmFlatRateUpTo: 15, pricePerKm: 0.40, kmMultiplier: 4 },
      { locationId: rostock.id, model: 'MAGIC_MIRROR', basePrice: 579, kmFlatRate: 60, kmFlatRateUpTo: 15, pricePerKm: 0.40, kmMultiplier: 4 },
    ],
    skipDuplicates: true,
  });

  await prisma.photobox.createMany({
    data: [
      { locationId: luebeck.id, model: 'VINTAGE_SMILE', serialNumber: 'LUE-VS-001' },
      { locationId: luebeck.id, model: 'VINTAGE_SMILE', serialNumber: 'LUE-VS-002' },
      { locationId: luebeck.id, model: 'VINTAGE_PHOTOS', serialNumber: 'LUE-VP-001' },
      { locationId: luebeck.id, model: 'NOSTALGIE', serialNumber: 'LUE-NOS-001' },
      { locationId: luebeck.id, model: 'MAGIC_MIRROR', serialNumber: 'LUE-MM-001' },
      
      { locationId: hamburg.id, model: 'VINTAGE_SMILE', serialNumber: 'HAM-VS-001' },
      { locationId: hamburg.id, model: 'VINTAGE_SMILE', serialNumber: 'HAM-VS-002' },
      { locationId: hamburg.id, model: 'VINTAGE_PHOTOS', serialNumber: 'HAM-VP-001' },
      { locationId: hamburg.id, model: 'NOSTALGIE', serialNumber: 'HAM-NOS-001' },
      { locationId: hamburg.id, model: 'MAGIC_MIRROR', serialNumber: 'HAM-MM-001' },
      
      { locationId: kiel.id, model: 'VINTAGE_SMILE', serialNumber: 'KIEL-VS-001' },
      { locationId: kiel.id, model: 'VINTAGE_PHOTOS', serialNumber: 'KIEL-VP-001' },
      { locationId: kiel.id, model: 'NOSTALGIE', serialNumber: 'KIEL-NOS-001' },
      
      { locationId: berlin.id, model: 'VINTAGE_SMILE', serialNumber: 'BER-VS-001' },
      { locationId: berlin.id, model: 'MAGIC_MIRROR', serialNumber: 'BER-MM-001' },
      
      { locationId: rostock.id, model: 'VINTAGE_SMILE', serialNumber: 'ROS-VS-001' },
      { locationId: rostock.id, model: 'NOSTALGIE', serialNumber: 'ROS-NOS-001' },
    ],
    skipDuplicates: true,
  });

  console.log('Database seeded successfully!');
  console.log('\nTest Accounts:');
  console.log('Admin: admin@savethemoment.de / admin123');
  console.log('Fahrer 1: fahrer1@savethemoment.de / driver123');
  console.log('Fahrer 2: fahrer2@savethemoment.de / driver123');
}

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