import Comparison from "./comparison";
import { getStaticPage } from "../../../lib/directQueries/allqueries";
import FAQSchema from "../../components/FAQSchema";

const PAGE_SLUG = 'comparison';

export async function generateMetadata() {

  const response = await getStaticPage(PAGE_SLUG);
  
  const page = response?.data;

  return {
    title: page?.meta_title || "University & Course Comparison: Find Your Best Match",
    description: page?.meta_description || "Compare top universities and courses in one place. Check fees and leading scholarships to ensure you make the right choice for your career.",
    openGraph: {
      title: page?.meta_title || "University & Course Comparison: Find Your Best Match",
      description: page?.meta_description || "Compare top universities and courses in one place. Check fees and leading scholarships to ensure you make the right choice for your career.",
      url: `${process.env.NEXT_PUBLIC_APP_URL}/${PAGE_SLUG}`,
      siteName: process.env.NEXT_PUBLIC_APP_WEBSITE_NAME,
      locale: "en_US",
      type: "website",
    },
    twitter: {
      card: "summary_large_image",
      title: page?.meta_title || "University & Course Comparison: Find Your Best Match",
      description: page?.meta_description || "Compare top universities and courses in one place. Check fees and leading scholarships to ensure you make the right choice for your career.",
    },
    alternates: {
      canonical: `${process.env.NEXT_PUBLIC_APP_URL}/${PAGE_SLUG}`,
    },
    robots: {
      index: true,
      follow: true,
      nocache: false,
      googleBot: {
        index: true,
        follow: true,
        noimageindex: false,
        "max-video-preview": -1,
        "max-image-preview": "large",
        "max-snippet": -1,
      },
    },
  };
}

async function fetchData() {
  const pageResponse = await getStaticPage(PAGE_SLUG);

  let faqs = [];
  try {
    const raw = pageResponse?.data?.faqs;
    faqs = raw ? JSON.parse(raw) : [];
  } catch {
    faqs = [];
  }

  return {
    pageData: pageResponse?.data || null,
    faqs,
  };
}

export default async function ComparisonPage() {
  const { pageData, faqs } = await fetchData();

  return (
    <>
      <FAQSchema faqs={faqs} />
      <Comparison
        pageData={pageData}
        faqs={faqs}
      />
    </>
  );
}