import { getFeaturedStudentReviews, getFeaturedSuccessStories, getFeaturedPartners, getStaticPage } from "../../../lib/directQueries/allqueries";
import PartnersSection from "../../components/organisms/PartnersSection";
import StudentReviews from "../../components/organisms/StudentReviews";
import SuccessStories from "../../components/organisms/SuccessStories";
import HeroSection from "../../components/HeroSection";
import CountriesGrid from "../../components/CountriesGrid";
import CTASection from "../../components/CTASection";
import CitiesGrid from "../../components/CitiesGrid";
import TextDisplay from "../../components/clientcomponents/TextDisplay";

const PAGE_SLUG = 'study-abroad-consultants';

// ✅ Dynamic metadata from DB
export async function generateMetadata() {
  const response = await getStaticPage(PAGE_SLUG);
  const page = response?.data;

  return {
    title: page?.meta_title || "Trusted Study Abroad Consultants in Pakistan",
    description: page?.meta_description || "Looking for trusted study abroad consultants in Pakistan? Expert guidance on universities, scholarships, visas, and applications.",
    openGraph: {
      title: page?.meta_title || "Trusted Study Abroad Consultants in Pakistan",
      description: page?.meta_description || "Looking for trusted study abroad consultants in Pakistan?",
      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 || "Trusted Study Abroad Consultants in Pakistan",
      description: page?.meta_description || "Looking for trusted study abroad consultants in Pakistan?",
    },
    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 [reviewsData, storiesData, partnersData, pageData] = await Promise.all([
    getFeaturedStudentReviews(),
    getFeaturedSuccessStories(),
    getFeaturedPartners(),
    getStaticPage(PAGE_SLUG)      // ✅ reusable, slug-based
  ]);

  return {
    storiesData,
    reviewsData: reviewsData.data || [],
    partnersData: partnersData.data || [],
    pageData: pageData.data || null
  };
}

export default async function StudyAbroadConsultantsPage() {
  try {
    const { storiesData, reviewsData, partnersData, pageData } = await fetchData();

    return (
      <>
        <HeroSection
          toptitle="International Education Advisory Services"
          simple_title={pageData?.simple_title}
          color_title={pageData?.color_title}
          description={pageData?.short_description}
        />
        <CountriesGrid />
        <CTASection
          title="Your future, guided by"
          midtitle="Trusted"
          endtitle="Study Abroad Consultants"
        />
        <CitiesGrid />
        <PartnersSection partnersData={partnersData} />
        <StudentReviews reviewsData={reviewsData} />
        <SuccessStories stories={storiesData.data} />
        <div className="py-12 md:py-16 lg:py-20">
          <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div className="bg-white rounded-2xl shadow-xl overflow-hidden border border-gray-200">
              <div className="p-6 md:p-8 lg:p-12">
                <TextDisplay
                  content={pageData?.bottom_content}
                  className="prose prose-lg max-w-none sn-content"
                />
              </div>
            </div>
          </div>
        </div>
      </>
    );

  } catch (error) {
    console.error('Error in StudyAbroadConsultantsPage:', error);
  }
}