import { PrismaClient } from "@prisma/client";
import bcrypt from "bcryptjs";

const prisma = new PrismaClient();

async function main() {
  const adminEmail = process.env.ADMIN_EMAIL || "admin@example.com";
  const adminPassword = process.env.ADMIN_PASSWORD || "Admin123!";
  const hashedPassword = await bcrypt.hash(adminPassword, 12);

  await prisma.user.upsert({
    where: { email: adminEmail },
    update: {},
    create: {
      email: adminEmail,
      name: "Admin",
      password: hashedPassword,
      role: "ADMIN",
    },
  });

  await prisma.siteSettings.upsert({
    where: { id: "default" },
    update: {},
    create: { id: "default" },
  });

  const services = [
    { name: "Emergency Plumbing", description: "24/7 emergency plumbing services for burst pipes, overflows, and urgent repairs.", icon: "droplets", sortOrder: 0 },
    { name: "Leak Repair", description: "Fast detection and repair of water leaks to prevent damage and save money.", icon: "wrench", sortOrder: 1 },
    { name: "Drain Cleaning", description: "Professional drain cleaning using advanced equipment for clogged drains.", icon: "droplets", sortOrder: 2 },
    { name: "Water Heater Repair", description: "Expert repair and installation of tank and tankless water heaters.", icon: "flame", sortOrder: 3 },
    { name: "Pipe Installation", description: "New pipe installation and repiping services for homes and businesses.", icon: "wrench", sortOrder: 4 },
    { name: "Commercial Plumbing", description: "Full-service commercial plumbing for offices, restaurants, and retail.", icon: "shield", sortOrder: 5 },
  ];

  for (const service of services) {
    const existing = await prisma.service.findFirst({ where: { name: service.name } });
    if (!existing) {
      await prisma.service.create({ data: service });
    }
  }

  const whyChooseUs = [
    { title: "Same Day Service", description: "We respond quickly and often complete jobs the same day you call.", icon: "clock", sortOrder: 0 },
    { title: "Licensed Technicians", description: "All our technicians are fully licensed, trained, and background-checked.", icon: "shield", sortOrder: 1 },
    { title: "Upfront Pricing", description: "No hidden fees. You'll know the cost before we start any work.", icon: "dollar", sortOrder: 2 },
    { title: "Satisfaction Guarantee", description: "We stand behind our work with a 100% satisfaction guarantee.", icon: "award", sortOrder: 3 },
    { title: "24/7 Availability", description: "Emergencies don't wait. Neither do we. Call us anytime, day or night.", icon: "phone", sortOrder: 4 },
    { title: "Fully Insured", description: "Complete insurance coverage for your peace of mind and protection.", icon: "check", sortOrder: 5 },
  ];

  for (const item of whyChooseUs) {
    const existing = await prisma.whyChooseUs.findFirst({ where: { title: item.title } });
    if (!existing) {
      await prisma.whyChooseUs.create({ data: item });
    }
  }

  const testimonials = [
    { name: "Sarah Johnson", rating: 5, review: "Absolutely fantastic service! They arrived within an hour and fixed our burst pipe quickly. Professional, courteous, and fairly priced. Highly recommend!", location: "Downtown", sortOrder: 0 },
    { name: "Mike Chen", rating: 5, review: "Best plumbing company in the area. They've handled multiple jobs for our restaurant and always deliver quality work on time. Our go-to plumbers!", location: "Business District", sortOrder: 1 },
    { name: "Emily Rodriguez", rating: 5, review: "Called for an emergency at 2 AM and they were here in 30 minutes. Fixed the issue and saved us from major water damage. True lifesavers!", location: "Westside", sortOrder: 2 },
  ];

  for (const t of testimonials) {
    const existing = await prisma.testimonial.findFirst({ where: { name: t.name } });
    if (!existing) {
      await prisma.testimonial.create({ data: t });
    }
  }

  const serviceAreas = [
    { city: "Islamabad", area: "F-6, F-7, F-8", sortOrder: 0 },
    { city: "Rawalpindi", area: "Saddar, Bahria Town", sortOrder: 1 },
    { city: "Bahria Town", area: "Phase 1-8", sortOrder: 2 },
    { city: "DHA", area: "Phase 1-5", sortOrder: 3 },
    { city: "Gulberg", area: "Main Boulevard", sortOrder: 4 },
  ];

  for (const area of serviceAreas) {
    const existing = await prisma.serviceArea.findFirst({ where: { city: area.city, area: area.area } });
    if (!existing) {
      await prisma.serviceArea.create({ data: area });
    }
  }

  const faqs = [
    { question: "Do you offer emergency plumbing services?", answer: "Yes! We provide 24/7 emergency plumbing services. Call us anytime and our team will respond as quickly as possible, typically within 60 minutes.", sortOrder: 0 },
    { question: "How much do your services cost?", answer: "We provide upfront, transparent pricing before starting any work. Costs vary depending on the job, but we offer free estimates for most services. No hidden fees, ever.", sortOrder: 1 },
    { question: "Are your plumbers licensed and insured?", answer: "Absolutely. All our technicians are fully licensed, bonded, and insured. We carry comprehensive liability insurance for your protection.", sortOrder: 2 },
    { question: "What areas do you serve?", answer: "We serve Islamabad, Rawalpindi, Bahria Town, DHA, Gulberg, and surrounding areas. Contact us to confirm service availability in your location.", sortOrder: 3 },
    { question: "Do you offer warranties on your work?", answer: "Yes, we stand behind all our work with a satisfaction guarantee. Most repairs come with a warranty period. Ask your technician for specific warranty details.", sortOrder: 4 },
  ];

  for (const faq of faqs) {
    const existing = await prisma.fAQ.findFirst({ where: { question: faq.question } });
    if (!existing) {
      await prisma.fAQ.create({ data: faq });
    }
  }

  console.log("Seed completed successfully!");
  console.log(`Admin login: ${adminEmail} / ${adminPassword}`);
}

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