import { PrismaClient } from "@prisma/client";
import { PrismaMariaDb } from "@prisma/adapter-mariadb";
import bcrypt from "bcryptjs";

const adapter = new PrismaMariaDb({
  host: "localhost",
  port: 3306,
  user: "root",
  password: "",
  database: "restro_db",
  connectionLimit: 5,
});

const prisma = new PrismaClient({ adapter });

async function main() {
  // Seed admin user
  const hashedPassword = await bcrypt.hash("admin123", 12);
  await prisma.user.upsert({
    where: { email: "admin@restro.dk" },
    update: {},
    create: {
      email: "admin@restro.dk",
      password: hashedPassword,
      name: "Admin",
      role: "ADMIN",
    },
  });

  // Seed categories
  const starters = await prisma.category.upsert({
    where: { slug: "starters" },
    update: {},
    create: { name: "Starters", slug: "starters", description: "Begin your meal with our light and flavorful starters", sortOrder: 1 },
  });
  const mains = await prisma.category.upsert({
    where: { slug: "main-courses" },
    update: {},
    create: { name: "Main Courses", slug: "main-courses", description: "Hearty dishes crafted with passion", sortOrder: 2 },
  });
  const desserts = await prisma.category.upsert({
    where: { slug: "desserts" },
    update: {},
    create: { name: "Desserts", slug: "desserts", description: "Sweet endings to a perfect meal", sortOrder: 3 },
  });
  const drinks = await prisma.category.upsert({
    where: { slug: "drinks" },
    update: {},
    create: { name: "Drinks", slug: "drinks", description: "Refresh yourself with our curated beverages", sortOrder: 4 },
  });

  // Seed menu items
  const menuItems = [
    { name: "Smoked Salmon Tartare", slug: "smoked-salmon-tartare", description: "Fresh Atlantic salmon with capers, dill, and lemon zest on rye crisps.", price: 125, categoryId: starters.id, isFeatured: true, imageUrl: "https://images.unsplash.com/photo-1467003909585-2f8a72700288?w=600&q=80" },
    { name: "Beetroot Carpaccio", slug: "beetroot-carpaccio", description: "Thinly sliced beetroot with goat cheese mousse, walnuts, and honey.", price: 98, categoryId: starters.id, isFeatured: false, imageUrl: "https://images.unsplash.com/photo-1541014741259-de529411b96a?w=600&q=80" },
    { name: "Wild Mushroom Soup", slug: "wild-mushroom-soup", description: "Creamy soup with foraged Danish mushrooms and truffle oil.", price: 110, categoryId: starters.id, isFeatured: false, imageUrl: "https://images.unsplash.com/photo-1547592166-23ac45744acd?w=600&q=80" },
    { name: "Pan-Seared Duck Breast", slug: "pan-seared-duck-breast", description: "Succulent duck breast with cherry reduction, roasted root vegetables and potato gratin.", price: 285, categoryId: mains.id, isFeatured: true, imageUrl: "https://images.unsplash.com/photo-1432139509613-5c4255a1d197?w=600&q=80" },
    { name: "Grilled Atlantic Cod", slug: "grilled-atlantic-cod", description: "Perfectly grilled cod fillet with dill butter sauce and seasonal greens.", price: 265, categoryId: mains.id, isFeatured: true, imageUrl: "https://images.unsplash.com/photo-1534766555764-ce878a4e947d?w=600&q=80" },
    { name: "Danish Beef Tenderloin", slug: "danish-beef-tenderloin", description: "Premium Danish beef with red wine jus and hand-cut fries.", price: 325, categoryId: mains.id, isFeatured: true, imageUrl: "https://images.unsplash.com/photo-1558030006-450675393462?w=600&q=80" },
    { name: "Risotto Primavera", slug: "risotto-primavera", description: "Creamy arborio rice with seasonal vegetables and parmesan.", price: 195, categoryId: mains.id, isFeatured: false, imageUrl: "https://images.unsplash.com/photo-1476124369491-e7addf5db371?w=600&q=80" },
    { name: "Apple Crumble", slug: "apple-crumble", description: "Warm Danish apple crumble with vanilla ice cream and cinnamon.", price: 95, categoryId: desserts.id, isFeatured: true, imageUrl: "https://images.unsplash.com/photo-1568571780765-9276ac8b75a2?w=600&q=80" },
    { name: "Chocolate Fondant", slug: "chocolate-fondant", description: "Rich dark chocolate lava cake with raspberry sorbet.", price: 115, categoryId: desserts.id, isFeatured: true, imageUrl: "https://images.unsplash.com/photo-1606313564200-e75d5e30476c?w=600&q=80" },
    { name: "Craft Lager", slug: "craft-lager", description: "Locally brewed Danish craft lager, crisp and refreshing.", price: 65, categoryId: drinks.id, isFeatured: false, imageUrl: "https://images.unsplash.com/photo-1535958636474-b021ee887b13?w=600&q=80" },
    { name: "House Red Wine", slug: "house-red-wine", description: "Full-bodied Cabernet Sauvignon, selected by our sommelier.", price: 85, categoryId: drinks.id, isFeatured: false, imageUrl: "https://images.unsplash.com/photo-1510812431401-41d2bd2722f3?w=600&q=80" },
  ];

  for (const item of menuItems) {
    await prisma.menuItem.upsert({
      where: { slug: item.slug },
      update: {},
      create: item,
    });
  }

  // Seed job postings
  const jobs = [
    { title: "Head Chef", slug: "head-chef", department: "Kitchen", type: "Full-time", description: "We are looking for an experienced Head Chef to lead our kitchen team and create innovative seasonal menus that reflect our commitment to quality Danish cuisine.", requirements: "Minimum 5 years experience in fine dining\nCulinary degree preferred\nStrong leadership and team management skills\nPassion for Nordic cuisine", salary: "45,000 - 55,000 DKK/month" },
    { title: "Front of House Manager", slug: "front-of-house-manager", department: "Service", type: "Full-time", description: "Join our team as Front of House Manager. You will oversee the dining experience, manage our service staff, and ensure every guest leaves with a memorable experience.", requirements: "3+ years in restaurant management\nExcellent communication skills\nFluent in Danish and English\nFine dining experience preferred", salary: "35,000 - 42,000 DKK/month" },
    { title: "Sous Chef", slug: "sous-chef", department: "Kitchen", type: "Full-time", description: "Support our Head Chef in daily kitchen operations. This is a great opportunity for an ambitious chef looking to grow in an innovative culinary environment.", requirements: "2+ years as Sous Chef or equivalent\nKnowledge of modern cooking techniques\nAbility to work under pressure\nFood safety certification", salary: "32,000 - 38,000 DKK/month" },
    { title: "Weekend Server", slug: "weekend-server", department: "Service", type: "Part-time", description: "We need enthusiastic weekend servers to join our front-of-house team. Perfect for students or anyone seeking flexible part-time work in a premium dining environment.", requirements: "Previous serving experience preferred\nAvailable on Fridays, Saturdays and Sundays\nGreat communication skills\nTeam-oriented", salary: "175 DKK/hour" },
  ];

  for (const job of jobs) {
    await prisma.jobPosting.upsert({
      where: { slug: job.slug },
      update: {},
      create: job,
    });
  }

  // Seed testimonials
  const testimonials = [
    { name: "Maria Jensen", role: "Food Blogger", rating: 5, content: "Absolutely stunning experience! The duck breast was cooked to perfection and the wine pairing was exceptional. This is easily the best restaurant in the area.", isVisible: true },
    { name: "Lars Andersen", role: "Regular Guest", rating: 5, content: "We have been coming here for years and the quality never disappoints. The team makes you feel like family. The new seasonal menu is their best yet!", isVisible: true },
    { name: "Sophie Müller", role: "Tourist from Germany", rating: 4, content: "Found this gem during our trip to Denmark. The atmosphere was warm and inviting, and the smoked salmon tartare was a revelation. Will definitely return!", isVisible: true },
    { name: "Thomas Nielsen", role: "Corporate Events", rating: 5, content: "Hosted our company dinner here and it was flawless. From the private dining room to the custom menu, every detail was handled with care. Highly recommended!", isVisible: true },
  ];

  for (const t of testimonials) {
    await prisma.testimonial.create({ data: t });
  }

  // Seed settings
  const settings = [
    { key: "hero_title", value: "A Taste of Excellence" },
    { key: "hero_subtitle", value: "Experience culinary mastery with our seasonal dishes crafted from locally sourced ingredients." },
    { key: "about_text", value: "Restro Denmark has been serving exceptional cuisine since 2015. Our passion for quality ingredients and innovative techniques has made us a beloved destination for food lovers across Denmark." },
    { key: "address", value: "Nyhavn 42, 1051 Copenhagen, Denmark" },
    { key: "phone", value: "+45 33 12 34 56" },
    { key: "email", value: "info@restro.dk" },
  ];

  for (const s of settings) {
    await prisma.setting.upsert({
      where: { key: s.key },
      update: { value: s.value },
      create: s,
    });
  }

  console.log("Database seeded successfully!");
}

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