import type { Metadata } from "next";
import { prisma } from "@/lib/prisma";

type LayoutProps = {
  children: React.ReactNode;
  params: Promise<{
    slug: string;
  }>;
};

export async function generateMetadata({
  params,
}: LayoutProps): Promise<Metadata> {
  const { slug } = await params;

  try {
    const article = await prisma.article.findFirst({
      where: {
        slug,
        published: true,
      },
      select: {
        title: true,
        slug: true,
        excerpt: true,
        thumbnail: true,
        category: true,
        publishedAt: true,
        author: {
          select: {
            name: true,
          },
        },
      },
    });

    if (!article) {
      return {
        title: "Article Not Found | GlobalValueTech",
        description:
          "The requested article could not be found on GlobalValueTech.",
        robots: {
          index: false,
          follow: false,
        },
      };
    }

    const title = `${article.title} | GlobalValueTech`;

    const description =
      article.excerpt ||
      `Read ${article.title} by ${
        article.author?.name || "Eky Syafputra"
      } on GlobalValueTech.`;

    const canonicalUrl = `https://globalvaluetech.com/blog/${article.slug}`;

    return {
      title,

      description,

      keywords: [
        "GlobalValueTech",
        "GloVaTech",
        "Eky Syafputra",
        "Eky Syafputra Engineering Director",
        "Engineering Director",
        "Engineering Leadership",
        "Engineering Management",
        "Strategic Planning",
        "Business Continuity Management",
        "OPEX Management",
        "CAPEX Management",
        "Energy Management",
        "Energy Optimization",
        "IoT",
        "Internet of Things",
        "CMMS",
        "Building Management System",
        "BMS",
        "MEP Engineering",
        "Engineering Operations",
        "Technology Management",
        "Digital Transformation",
        "Engineering Insights",
        "Professional Portfolio",
        article.title,
        ...(article.category ? [article.category] : []),
      ],

      authors: [
        {
          name: article.author?.name || "Eky Syafputra",
        },
      ],

      alternates: {
        canonical: canonicalUrl,
      },

      openGraph: {
        type: "article",

        url: canonicalUrl,

        title,

        description,

        siteName: "GlobalValueTech",

        locale: "en_US",

        ...(article.publishedAt
          ? {
              publishedTime: article.publishedAt.toISOString(),
            }
          : {}),

        authors: [
          article.author?.name || "Eky Syafputra",
        ],

        ...(article.thumbnail
          ? {
              images: [
                {
                  url: article.thumbnail,
                  alt: article.title,
                },
              ],
            }
          : {}),
      },

      twitter: {
        card: article.thumbnail
          ? "summary_large_image"
          : "summary",

        title,

        description,

        ...(article.thumbnail
          ? {
              images: [article.thumbnail],
            }
          : {}),
      },

      robots: {
        index: true,

        follow: true,

        googleBot: {
          index: true,
          follow: true,
          "max-image-preview": "large",
          "max-snippet": -1,
          "max-video-preview": -1,
        },
      },
    };
  } catch (error) {
    console.error(
      "ARTICLE METADATA ERROR:",
      error
    );

    return {
      title: "GlobalValueTech | GloVaTech",
      description:
        "Professional articles and engineering insights by Eky Syafputra.",
    };
  }
}

export default function ArticleLayout({
  children,
}: LayoutProps) {
  return <>{children}</>;
}

