import graphql from "src/utils/graphql";
import CategoriesData from "src/view/Categories";


const gql_metadata = `
query CategoriesAndBrands($locale:I18NLocaleCode,$slug:String!) {
    categories(filters: { slug: { eqi: $slug } }, locale: $locale) {
        seo {
            id
            metaTitle
            metaDescription
            metaRobots
            canonicalURL
            metaImage {
                documentId
                alternativeText
                url
            }
            openGraph {
                id
                ogTitle
                ogDescription
                ogUrl
                ogType
                ogImage {
                    documentId
                    alternativeText
                    url
                }
            }
            structuredData
        }
    }
    brands(filters: { slug: { eqi: $slug } }, locale: $locale) {
        seo {
            id
            metaTitle
            metaDescription
            metaRobots
            canonicalURL
            metaImage {
                documentId
                alternativeText
                url
            }
            openGraph {
                id
                ogTitle
                ogDescription
                ogUrl
                ogType
                ogImage {
                    documentId
                    alternativeText
                    url
                }
            }
            structuredData
        }
    }
}

`
export async function generateMetadata({ params }) {
    const { locale, category } = await params;

    // Fetch product SEO data from Strapi GraphQL API
    const data = await graphql(gql_metadata, {
        locale,
        slug:category,
    });

    const productSEO = data?.categories?.[0]?.seo || data?.brands?.[0]?.seo;
    if (productSEO) {
        return {
            title: productSEO.metaTitle,
            description: productSEO.metaDescription,
            robots: productSEO.metaRobots || 'index, follow',
            // canonical: productSEO.canonicalURL || `https://adhorizonintl.com/product/${slug}`,
            alternates: {
                canonical: productSEO.canonicalURL || `https://adhorizonintl.com/products/${slug}`,
                languages: {
                  'en': `https://adhorizonintl.com/products/${slug}`,
                  'ar-OM': `https://adhorizonintl.com/ar-OM/products/${slug}`,
                },
              },
            openGraph: {
                title: productSEO.openGraph.ogTitle || productSEO.metaTitle,
                description: productSEO.openGraph.ogDescription || productSEO.metaDescription,
                url: productSEO.openGraph.ogUrl || `https://adhorizonintl.com/products/${slug}`,
                type: productSEO.openGraph.ogType || 'website',
                images: [
                    {
                        url: productSEO.openGraph.ogImage.url,
                        alt: productSEO.openGraph.ogImage.alternativeText || 'Product Image',
                    },
                ],
            },
            twitter: {
                card: 'summary_large_image',
                title: productSEO.metaTitle,
                description: productSEO.metaDescription,
                images: [productSEO.metaImage?.url],
            },
            // structuredData: productSEO.structuredData,
        };
    }
    else {
        return {}
    }
}

const CategoryPage = async ({ params }) => {
  const _params = await params;
  return (
    <CategoriesData category={_params.category ?? undefined} />
  );
};

export default CategoryPage;
