---
import { Font } from "astro:assets";
import { ClientRouter } from "astro:transitions";
import { PUBLIC_GOOGLE_SITE_VERIFICATION } from "astro:env/client";
import { SITE } from "@/config";
import "@/styles/global.css";

type Props = {
  title?: string;
  author?: string;
  profile?: string;
  description?: string;
  ogImage?: string;
  canonicalURL?: string;
  pubDatetime?: Date;
  modDatetime?: Date | null;
  scrollSmooth?: boolean;
};

const {
  title = SITE.title,
  author = SITE.author,
  profile = SITE.profile,
  description = SITE.desc,
  ogImage = SITE.ogImage ? `/${SITE.ogImage}` : "/og.png",
  canonicalURL = new URL(Astro.url.pathname, Astro.url),
  pubDatetime,
  modDatetime,
  scrollSmooth = false,
} = Astro.props;

const socialImageURL = new URL(ogImage, Astro.url);

const structuredData = {
  "@context": "https://schema.org",
  "@type": "BlogPosting",
  headline: `${title}`,
  image: `${socialImageURL}`,
  datePublished: `${pubDatetime?.toISOString()}`,
  ...(modDatetime && { dateModified: modDatetime.toISOString() }),
  author: [
    {
      "@type": "Person",
      name: `${author}`,
      ...(profile && { url: profile }),
    },
  ],
};
---

<!doctype html>
<html
  dir={SITE.dir}
  lang=`${SITE.lang ?? "en"}`
  class={`${scrollSmooth && "scroll-smooth"}`}
>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width" />
    <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
    <link rel="canonical" href={canonicalURL} />
    <meta name="generator" content={Astro.generator} />

    <!-- Load Font -->
    <Font
      cssVariable="--font-google-sans-code"
      preload={[{ subset: "latin", weight: 400, style: "normal" }]}
    />

    <!-- General Meta Tags -->
    <title>{title}</title>
    <meta name="title" content={title} />
    <meta name="description" content={description} />
    <meta name="author" content={author} />
    <link rel="sitemap" href="/sitemap-index.xml" />

    <!-- Open Graph / Facebook -->
    <meta property="og:title" content={title} />
    <meta property="og:description" content={description} />
    <meta property="og:url" content={canonicalURL} />
    <meta property="og:image" content={socialImageURL} />

    <!-- Article Published/Modified time -->
    {
      pubDatetime && (
        <meta
          property="article:published_time"
          content={pubDatetime.toISOString()}
        />
      )
    }
    {
      modDatetime && (
        <meta
          property="article:modified_time"
          content={modDatetime.toISOString()}
        />
      )
    }

    <!-- Twitter -->
    <meta property="twitter:card" content="summary_large_image" />
    <meta property="twitter:url" content={canonicalURL} />
    <meta property="twitter:title" content={title} />
    <meta property="twitter:description" content={description} />
    <meta property="twitter:image" content={socialImageURL} />

    <!-- Google JSON-LD Structured data -->
    <script
      type="application/ld+json"
      is:inline
      set:html={JSON.stringify(structuredData)}
    />

    <!-- Enable RSS feed auto-discovery  -->
    <!-- https://docs.astro.build/en/recipes/rss/#enabling-rss-feed-auto-discovery -->
    <link
      rel="alternate"
      type="application/rss+xml"
      title={SITE.title}
      href={new URL("rss.xml", Astro.site)}
    />

    <meta name="theme-color" content="" />

    {
      // If PUBLIC_GOOGLE_SITE_VERIFICATION is set in the environment variable,
      // include google-site-verification tag in the heading
      // Learn more: https://support.google.com/webmasters/answer/9008080#meta_tag_verification&zippy=%2Chtml-tag
      PUBLIC_GOOGLE_SITE_VERIFICATION && (
        <meta
          name="google-site-verification"
          content={PUBLIC_GOOGLE_SITE_VERIFICATION}
        />
      )
    }

    <ClientRouter />

    <!-- Minimal inline script to prevent FOUC - sets theme immediately -->
    <script is:inline>
      (function () {
        const initialColorScheme = ""; // "light" | "dark"
        const currentTheme = localStorage.getItem("theme");

        function getPreferTheme() {
          if (currentTheme) return currentTheme;
          if (initialColorScheme) return initialColorScheme;
          return window.matchMedia("(prefers-color-scheme: dark)").matches
            ? "dark"
            : "light";
        }

        const themeValue = getPreferTheme();

        // Set theme immediately to prevent flash
        document.firstElementChild?.setAttribute("data-theme", themeValue);

        // Export minimal API for external script
        window.theme = {
          themeValue: themeValue,
          getTheme: () => window.theme.themeValue,
          setTheme: val => {
            window.theme.themeValue = val;
          },
        };
      })();
    </script>
  </head>
  <body>
    <slot />

    <!-- Load full theme logic -->
    <script src="../scripts/theme.ts"></script>
  </body>
</html>
