import { getClient } from "@/lib/apolloClient";
import { gql } from "@apollo/client";

import PageHero from "@/components/PageHero";
import FAQSection from "@/components/FAQSection";
import { getFaqsByAccordionId } from "@/lib/getFaqs";
import GetInTouch from "@/components/GetInTouch";
import SolutionsSection from "@/components/single-service/SolutionsSection";
import WhyChoose from "@/components/single-service/WhyChoose";
import FeaturesGrid from "@/components/single-service/FeaturesGrid";
import BenefitsSection from "@/components/single-service/BenefitsSection";
import ServicesGrid from "@/components/single-service/ServicesGrid";
export const dynamic = "force-dynamic";
export const GAMING_PAGE_QUERY = gql`
  query GamingPage {
    pageBy(uri: "services/sports-app-development") {
      title
      uri
      content
      blogFaqSection {
        easyAccordionId
      }
      seo {
        title
        metaDesc
        canonical
        opengraphTitle
        opengraphDescription
        opengraphImage {
          sourceUrl
        }
      }

      featuredImage {
        node {
          sourceUrl
          altText
        }
      }

      gameDevelopment {
        solutionsSubtitle
        solutionsTitle
        solutionsDescription
        solutionsButtonLabel
        solutionsButtonLink

        servicesGridSubtitle
        solutionsGridTitle

        services1 { serviceTitle serviceDescription serviceIcon }
        services2 { serviceTitle serviceDescription serviceIcon }
        services3 { serviceTitle serviceDescription serviceIcon }
        services4 { serviceTitle serviceDescription serviceIcon }
        services5 { serviceTitle serviceDescription serviceIcon }
        services6 { serviceTitle serviceDescription serviceIcon }

        benefitsSubtitle
        benefitsTitle
        benefitsDescription
        benefitsItems {
          itemTitle1 itemDescription1
          itemTitle2 itemDescription2
          itemTitle3 itemDescription3
          itemTitle4 itemDescription4
          itemTitle5 itemDescription5
          itemTitle6 itemDescription6
        }

        whyChooseImage { node { sourceUrl altText } }
        whyChooseSubtitle
        whyChooseTitle
        whyChooseButtonLabel
        whyChooseButtonLink
        whyChooseItems {
          item1 itemDescription1
          item2 itemDescription2
          item3 itemDescription3
          item4 itemDescription4
          item5 itemDescription5
        }

        featuresSubtitle
        featuresTitle
        featuresItems {
          item1 itemDescription1
          item2 itemDescription2
          item3 itemDescription3
          item4 itemDescription4
          item5 itemDescription5
          item6 itemDescription6
        }
      }
    }
  }
`;


type MediaNode = {
  sourceUrl: string;
  altText?: string | null;
};

type ServiceItem = {
  serviceTitle?: string;
  serviceDescription?: string;
  serviceIcon?: string | null; // ✅ SVG string
};

type GameDevelopment = {
  solutionsSubtitle?: string;
  solutionsTitle?: string;
  solutionsDescription?: string;
  solutionsButtonLabel?: string;
  solutionsButtonLink?: string | null;

  servicesGridSubtitle?: string;
  solutionsGridTitle?: string;

  services1?: ServiceItem | null;
  services2?: ServiceItem | null;
  services3?: ServiceItem | null;
  services4?: ServiceItem | null;
  services5?: ServiceItem | null;
  services6?: ServiceItem | null;

  benefitsSubtitle?: string;
  benefitsTitle?: string;
  benefitsDescription?: string;
  benefitsItems: {
    itemTitle1?: string | null;
    itemDescription1?: string | null;
    itemTitle2?: string | null;
    itemDescription2?: string | null;
    itemTitle3?: string | null;
    itemDescription3?: string | null;
    itemTitle4?: string | null;
    itemDescription4?: string | null;
    itemTitle5?: string | null;
    itemDescription5?: string | null;
    itemTitle6?: string | null;
    itemDescription6?: string | null;
  };

  whyChooseImage?: { node?: MediaNode };
  whyChooseSubtitle?: string;
  whyChooseTitle?: string;
  whyChooseButtonLabel?: string;
  whyChooseButtonLink?: string | null;
  whyChooseItems?: Record<string, string | null>;

  featuresSubtitle?: string;
  featuresTitle?: string;
  featuresItems?: Record<string, string | null>;
};
type YoastSEO = {
  title?: string | null;
  metaDesc?: string | null;
  canonical?: string | null;
  opengraphTitle?: string | null;
  opengraphDescription?: string | null;
  opengraphImage?: {
    sourceUrl?: string | null;
  } | null;
};
type GamingPageQueryResult = {
  pageBy: {
    title: string;
    uri: string;

    blogFaqSection?: {
      easyAccordionId?: string | null;
    } | null;

    seo?: YoastSEO | null;
    content?: string;
    featuredImage?: {
      node?: MediaNode;
    };
    gameDevelopment?: GameDevelopment;
  } | null;
};

import { Metadata } from "next";

export async function generateMetadata(): Promise<Metadata> {
  const { data } = await getClient().query<GamingPageQueryResult>({
    query: GAMING_PAGE_QUERY,
    fetchPolicy: "no-cache",
  });

  const page = data?.pageBy;
  const seo = page?.seo;

  const canonical =
    seo?.canonical && seo.canonical.trim() !== ""
      ? seo.canonical
      : page?.uri || "/services/sports-app-development/";

  return {
    metadataBase: new URL("https://www.togwe.com"),

    title: seo?.title || "Sports App Development Services | Togwe",

    description:
      seo?.metaDesc ||
      "Custom sports app development services to build scalable, high-performance platforms.",

    alternates: {
      canonical,
    },

    openGraph: {
      title:
        seo?.opengraphTitle ||
        seo?.title ||
        "Sports App Development Services | Togwe",

      description:
        seo?.opengraphDescription || seo?.metaDesc || undefined,

      images: seo?.opengraphImage?.sourceUrl
        ? [{ url: seo.opengraphImage.sourceUrl }]
        : [],
    },
  };
}



export default async function GamingPlatformDevelopmentPage() {
  const { data } = await getClient().query<GamingPageQueryResult>({
    query: GAMING_PAGE_QUERY,
  });

  const page = data?.pageBy;
  const service = page?.gameDevelopment;

  if (!page || !service) return null;

  // ✅ Extract FAQ accordion ID dynamically
  const faqAccordionId = page.blogFaqSection?.easyAccordionId
    ? Number(page.blogFaqSection.easyAccordionId)
    : null;

  // ✅ Fetch FAQs only if ID exists
  const faqs = faqAccordionId
    ? await getFaqsByAccordionId(faqAccordionId)
    : [];

  return (
    <>
      <PageHero title={page.title} />

      <SolutionsSection
        data={service}
        featuredImage={page.featuredImage}
      />

      <ServicesGrid
        data={{
          servicesGridSubtitle: service.servicesGridSubtitle,
          solutionsGridTitle: service.solutionsGridTitle,
          services1: service.services1,
          services2: service.services2,
          services3: service.services3,
          services4: service.services4,
          services5: service.services5,
          services6: service.services6,
        }}
      />

      <BenefitsSection data={service} />

      <FeaturesGrid
        data={{
          featuresSubtitle: service.featuresSubtitle,
          featuresTitle: service.featuresTitle,
          featuresItems: service.featuresItems,
        }}
      />

      <WhyChoose data={service} />

      {faqs.length > 0 && <FAQSection faqs={faqs} />}

      <GetInTouch />
    </>
  );
}

