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

import PageHero from "@/components/PageHero";
import GetInTouch from "@/components/GetInTouch";
import SolutionsSection from "@/components/single-service/SolutionsSection";
import WhyChoose from "@/components/customer-support-services/WhyChoose";
import BenefitsSection from "@/components/customer-support-services/BenefitsSection";
import ServicesGrid from "@/components/customer-support-services/ServicesGrid";
export const dynamic = "force-dynamic";
export const CUSTOMER_SUPPORT_QUERY = gql`
  query CustomerSupportPage {
    pageBy(uri: "services/customer-support") {
      title
      uri

      seo {
        title
        metaDesc
        canonical
        opengraphTitle
        opengraphDescription
        opengraphImage {
          sourceUrl
        }
      }

      featuredImage {
        node {
          sourceUrl
          altText
        }
      }

      customerSupport {
        solutionsSubtitle
        solutionsTitle
        solutionsDescription
        solutionsButtonLabel
        solutionsButtonLink

        servicesGridSubtitle
        servicesGridTitle
        serviceGridDescription

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

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

        whyChooseSubtitle
        whyChooseTitle
        whyChooseItems {
          item1 itemDescription1
          item2 itemDescription2
          item3 itemDescription3
          item4 itemDescription4
          item5 itemDescription5
          item6 itemDescription6
          item7 itemDescription7
          item8 itemDescription8
          item9 itemDescription9
        }
      }
    }
  }
`;


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

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

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

  servicesGridSubtitle?: string;
  servicesGridTitle?: string;
  serviceGridDescription?: string;
  services1?: ServiceItem | null;
  services2?: ServiceItem | null;
  services3?: ServiceItem | null;
  services4?: ServiceItem | null;

  benefitsSubtitle?: string;
  benefitsTitle?: 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;
  };

  whyChooseSubtitle?: string;
  whyChooseTitle?: string;
  whyChooseItems?: 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 CustomerPageQueryResult = {
  pageBy: {
    title: string;
    uri: string;
    seo?: YoastSEO | null;
    content?: string;
    featuredImage?: {
      node?: MediaNode;
    };
    customerSupport?: CustomerSupport;
  } | null;
};

import { Metadata } from "next";

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

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

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

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

    title: seo?.title || "Customer Support Services | Togwe",

    description:
      seo?.metaDesc ||
      "Reliable customer support services to enhance customer satisfaction and retention.",

    alternates: {
      canonical,
    },

    openGraph: {
      title:
        seo?.opengraphTitle ||
        seo?.title ||
        "Customer Support Services | Togwe",

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

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


export default async function CustomerSupportServicesPage() {
  const { data } = await getClient().query<CustomerPageQueryResult>({
    query: CUSTOMER_SUPPORT_QUERY,
  });

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

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

  return (
    <>
      <PageHero title={page.title} />
      <SolutionsSection data={service} featuredImage={page.featuredImage} />
      <ServicesGrid
        data={{
          servicesGridSubtitle: service.servicesGridSubtitle,
          servicesGridTitle: service.servicesGridTitle,
          serviceGridDescription: service.serviceGridDescription,
          services1: service.services1,
          services2: service.services2,
          services3: service.services3,
          services4: service.services4,
        }}
      />
      <BenefitsSection data={service} />
      <WhyChoose data={service} />
      <GetInTouch />
    </>
  );
}
