import { PrismaClient } from "../app/generated/prisma/client";
import { PrismaBetterSqlite3 } from "@prisma/adapter-better-sqlite3";
import path from "node:path";
import fs from "node:fs";

const dbPath = path.join(process.cwd(), "data", "vidoschool.db");
fs.mkdirSync(path.dirname(dbPath), { recursive: true });

const adapter = new PrismaBetterSqlite3({ url: `file:${dbPath}` });
const prisma = new PrismaClient({ adapter });

const programs = [
  {
    title: "In-Center Tuition",
    description:
      "Structured in-person classes with expert guidance, collaborative learning, and steady academic support.",
    sortOrder: 0,
  },
  {
    title: "Online Tuition",
    description:
      "Live online lessons with digital resources and tutor support, giving students flexible learning from anywhere.",
    sortOrder: 1,
  },
  {
    title: "Home Tutoring",
    description:
      "Personalized one-on-one tutoring at home, tailored to each student's pace, strengths, and goals.",
    sortOrder: 2,
  },
  {
    title: "Holiday & Revision Programs",
    description:
      "Focused holiday coaching that closes learning gaps, reinforces key topics, and prepares students for exams.",
    sortOrder: 3,
  },
];

const blogs = [
  {
    title: "How Vido Learners Build Confidence in Every Class",
    category: "Featured Story",
    excerpt:
      "Discover how our in-center, online, and home tutoring pathways help learners stay engaged, improve results, and grow with confidence.",
    imageUrl: "/vido_galary/futureleaders.jpg",
    featured: true,
    sortOrder: 0,
  },
  {
    title: "Fine Arts Showcase",
    category: "School Events",
    excerpt:
      "A celebration of student creativity, performance, and artistic expression from our senior learners.",
    imageUrl: "",
    featured: false,
    sortOrder: 1,
  },
  {
    title: "Science and Engineering Fair",
    category: "Academic Highlights",
    excerpt:
      "Students presented practical science projects and innovative experiments during our annual fair.",
    imageUrl: "",
    featured: false,
    sortOrder: 2,
  },
  {
    title: "Mentorship and Career Day",
    category: "Career Guidance",
    excerpt:
      "Families connected with colleges and mentors to explore future academic and career opportunities.",
    imageUrl: "",
    featured: false,
    sortOrder: 3,
  },
];

const settings = [
  { key: "header_title", value: "Vido International" },
  { key: "header_subtitle", value: "Home School" },
  { key: "header_logo", value: "/vido_galary/logo-school.png" },

  { key: "hero_heading", value: "Discover Your Education Potential" },
  {
    key: "hero_description",
    value:
      "Our school provides dynamic environment that empowers students to new heights.",
  },
  { key: "hero_cta_text", value: "Explore More" },
  { key: "hero_cta_href", value: "/programs" },

  { key: "ged_heading", value: "American GED® partner in Kenya." },
  {
    key: "ged_body",
    value:
      "Earn a globally recognised US high school equivalency right here at Vido - your direct pathway to American universities, careers abroad, and opportunities worldwide.",
  },
  { key: "ged_seal_top", value: "Official" },
  { key: "ged_seal_main", value: "GED®" },
  { key: "ged_seal_bottom", value: "Partner · Kenya" },

  { key: "about_heading", value: "About Vido School" },
  {
    key: "about_body",
    value:
      "Vido International Home School provides flexible, quality education that empowers learners to grow academically, creatively, and confidently in a supportive home-based environment.",
  },
  { key: "about_cta_text", value: "Read More" },
  { key: "about_cta_href", value: "/about" },
  { key: "about_image_primary", value: "/vido_galary/kidslab.jpg" },
  { key: "about_image_secondary", value: "/vido_galary/kidsbag.jpg" },

  { key: "programs_heading", value: "Learning That Fits Your Child" },
  {
    key: "programs_body",
    value:
      "From in-center classes to online lessons and home tutoring, we support each student with the guidance they need to thrive.",
  },
  { key: "programs_image", value: "/vido_galary/kidslearning.png" },

  { key: "partners_label", value: "Our Partners" },

  { key: "blogs_heading", value: "Latest Updates from Vido School" },
  {
    key: "blogs_body",
    value: "News, highlights, and insights from our learning community.",
  },

  { key: "footer_title", value: "Vido International Home School" },
  {
    key: "footer_description",
    value:
      "Building confident learners through modern academics, mentorship, and a values-driven community.",
  },
  {
    key: "footer_address",
    value: "Omwami Close, Kitengela\nMon - Fri: 8:00 AM - 5:00 PM",
  },
  { key: "footer_phone", value: "+254 718 372 880" },
  { key: "footer_email", value: "info@vidoschool.com" },
  { key: "footer_programs_heading", value: "Programs" },
  { key: "footer_contact_heading", value: "Contact Us" },
];

const sections = [
  { key: "hero", label: "Hero", sortOrder: 0 },
  { key: "ged", label: "GED feature", sortOrder: 1 },
  { key: "about", label: "About", sortOrder: 2 },
  { key: "programs", label: "Programs", sortOrder: 3 },
  { key: "partners", label: "Partners", sortOrder: 4 },
  { key: "blogs", label: "Blog", sortOrder: 5 },
];

const heroSlides = [
  {
    section: "hero_slide",
    label: "Slide 1",
    imageUrl: "/vido_galary/futureleaders.jpg",
    altText: "Students learning",
    sortOrder: 0,
  },
  {
    section: "hero_slide",
    label: "Slide 2",
    imageUrl: "/vido_galary/teens.jpg",
    altText: "Creative students",
    sortOrder: 1,
  },
  {
    section: "hero_slide",
    label: "Slide 3",
    imageUrl: "/vido_galary/learning.jpg",
    altText: "Collaborative learning",
    sortOrder: 2,
  },
  {
    section: "hero_slide",
    label: "Slide 4",
    imageUrl: "/vido_galary/kids.jpg",
    altText: "Future leaders",
    sortOrder: 3,
  },
];

const partners = [
  {
    section: "partner",
    label: "CBC / KNEC",
    imageUrl: "/vido_galary/KNEC.png",
    altText: "CBC / KNEC logo",
    sortOrder: 0,
  },
  {
    section: "partner",
    label: "Cambridge IGCSE",
    imageUrl: "/vido_galary/IGCSE.png",
    altText: "Cambridge IGCSE logo",
    sortOrder: 1,
  },
  {
    section: "partner",
    label: "GED",
    imageUrl: "/vido_galary/GED.jpg",
    altText: "GED logo",
    sortOrder: 2,
  },
  {
    section: "partner",
    label: "Pearson Edexcel",
    imageUrl: "/vido_galary/pearson.png",
    altText: "Pearson Edexcel logo",
    sortOrder: 3,
  },
];

const footerPrograms = [
  {
    section: "footer_program",
    label: "STEM Academy",
    sortOrder: 0,
  },
  {
    section: "footer_program",
    label: "Creative Arts",
    sortOrder: 1,
  },
  {
    section: "footer_program",
    label: "Languages",
    sortOrder: 2,
  },
  {
    section: "footer_program",
    label: "Robotics & Coding",
    sortOrder: 3,
  },
];

async function seedItems(
  section: string,
  items: Array<{
    section: string;
    label: string;
    text?: string;
    href?: string;
    imageUrl?: string;
    altText?: string;
    sortOrder: number;
  }>,
) {
  const count = await prisma.homepageItem.count({ where: { section } });
  if (count > 0) return;
  await prisma.homepageItem.createMany({
    data: items.map((item) => ({
      section: item.section,
      label: item.label,
      text: item.text ?? "",
      href: item.href ?? "",
      imageUrl: item.imageUrl ?? "",
      altText: item.altText ?? "",
      sortOrder: item.sortOrder,
      published: true,
    })),
  });
  console.log(`Seeded ${items.length} ${section} items`);
}

async function main() {
  const programCount = await prisma.program.count();
  if (programCount === 0) {
    await prisma.program.createMany({ data: programs });
    console.log(`Seeded ${programs.length} programs`);
  }

  const blogCount = await prisma.blogPost.count();
  if (blogCount === 0) {
    await prisma.blogPost.createMany({ data: blogs });
    console.log(`Seeded ${blogs.length} blog posts`);
  }

  for (const setting of settings) {
    await prisma.siteSetting.upsert({
      where: { key: setting.key },
      update: {},
      create: setting,
    });
  }
  console.log(`Upserted ${settings.length} site settings`);

  for (const section of sections) {
    await prisma.homepageSection.upsert({
      where: { key: section.key },
      update: {},
      create: { ...section, visible: true },
    });
  }
  console.log(`Upserted ${sections.length} homepage sections`);

  await seedItems("hero_slide", heroSlides);
  await seedItems("partner", partners);
  await seedItems("footer_program", footerPrograms);
}

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