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";
import FAQSection from "../../components/FAQSection";
import FAQSchema from "../../components/FAQSchema";

const PAGE_SLUG = 'study-abroad-consultants-in-faisalabad';

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

  return {
    title: page?.meta_title || "Study Abroad Consultants in Faisalabad - Universities Page",
    description: page?.meta_description || "Universities Page, Pakistan's #01 Study Abroad Consultants in Faisalabad, provides personalized support for admissions, student visas, and scholarships abroad.",
    openGraph: {
      title: page?.meta_title || "Study Abroad Consultants in Faisalabad - Universities Page",
      description: page?.meta_description || "Universities Page, Pakistan's #01 Study Abroad Consultants in Faisalabad, provides personalized support for admissions, student visas, and scholarships abroad.",
      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 || "Study Abroad Consultants in Faisalabad - Universities Page",
      description: page?.meta_description || "Universities Page, Pakistan's #01 Study Abroad Consultants in Faisalabad, provides personalized support for admissions, student visas, and scholarships abroad.",
    },
    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, pageResponse] = await Promise.all([
    getFeaturedStudentReviews(),
    getFeaturedSuccessStories(),
    getFeaturedPartners(),
    getStaticPage(PAGE_SLUG),
  ]);

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

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

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

    return (
      <>
        <FAQSchema faqs={faqs} />
        <HeroSection
          toptitle="Professional Educational Consultant in Faisalabad for Global Opportunities"
          simple_title={pageData?.simple_title}
          color_title={pageData?.color_title}
          description={pageData?.short_description}
        />
        <PartnersSection partnersData={partnersData} />
        <CountriesGrid />
        <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?.top_content}
                  className="prose prose-lg max-w-none sn-content"
                />
              </div>
            </div>
          </div>
        </div>
        <CTASection
          title="Trusted"
          midtitle="Study Abroad Consultants"
          endtitle="in Faisalabad Pakistan"
        />
        <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?.middle_content}
                  className="prose prose-lg max-w-none sn-content"
                />
              </div>
            </div>
          </div>
        </div>
        <CitiesGrid />
        {faqs.length > 0 && <FAQSection faqs={faqs} />}
        <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 StudyAbroadConsultantFaisalabad:', error);
  }
}