import React from "react";
import EligibilityCalculator from "./EligibilityCalculator";
import { getCountries, getLevels, getSubjects, getStaticPage } from "../../../lib/directQueries/allqueries";

const PAGE_SLUG = 'eligibility-calculator';

export async function generateMetadata() {
  const response = await getStaticPage(PAGE_SLUG);
  const page = response?.data;

  return {
    title: page?.meta_title || "Best Study Abroad Eligibility Calculator: Try it for Free",
    description: page?.meta_description || "Find top universities matching your profile with our proven calculator. Check your study abroad eligibility, scholarship options, and visa success for free.",
    openGraph: {
      title: page?.meta_title || "Best Study Abroad Eligibility Calculator: Try it for Free",
      description: page?.meta_description || "Find top universities matching your profile with our proven calculator. Check your study abroad eligibility, scholarship options, and visa success for free.",
      url: `${process.env.NEXT_PUBLIC_APP_URL}/${PAGE_SLUG}`,
      siteName: process.env.NEXT_PUBLIC_APP_WEBSITE_NAME,
      type: "website",
    },
    twitter: {
      card: "summary_large_image",
      title: page?.meta_title || "Best Study Abroad Eligibility Calculator: Try it for Free",
      description: page?.meta_description || "Find top universities matching your profile with our proven calculator. Check your study abroad eligibility, scholarship options, and visa success for free.",
    },
    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,
      },
    },
  };
}

export default async function EligibilityPage() {
  const [countries, levels, subjects, pageResponse] = await Promise.all([
    getCountries(),
    getLevels(),
    getSubjects(),
    getStaticPage(PAGE_SLUG),
  ]);

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

  return (
    <EligibilityCalculator
      countries={countries}
      levels={levels}
      subjects={subjects}
      pageData={pageResponse?.data || null}
      faqs={faqs}
    />
  );
}