---
import siteConfig from '@/config/site.config';

interface Props {
  title?: string;
  description?: string;
  image?: string;
  imageAlt?: string;
  article?: {
    publishedTime?: Date;
    modifiedTime?: Date;
    authors?: string[];
    tags?: string[];
  };
  noindex?: boolean;
  nofollow?: boolean;
}

const {
  title,
  description = siteConfig.description,
  image,
  imageAlt,
  article,
  noindex = false,
  nofollow = false,
} = Astro.props;

const pageTitle = title ? `${title} — ${siteConfig.name}` : siteConfig.name;

const canonicalURL = new URL(Astro.url.pathname, Astro.site);

// Process image - fall back to the static default OG image
let ogImage: string;
if (image) {
  ogImage = image.startsWith('http') ? image : new URL(image, Astro.site).toString();
} else {
  ogImage = new URL(siteConfig.ogImage, Astro.site).toString();
}

const robotsContent = [noindex ? 'noindex' : 'index', nofollow ? 'nofollow' : 'follow'].join(', ');

// Normalize BCP-47 locale (e.g. "en") to OG language_TERRITORY format (e.g. "en_US")
const localeMap: Record<string, string> = {
  en: 'en_US', fr: 'fr_FR', de: 'de_DE', es: 'es_ES', it: 'it_IT',
  pt: 'pt_BR', nl: 'nl_NL', ja: 'ja_JP', ko: 'ko_KR', zh: 'zh_CN',
  ru: 'ru_RU', ar: 'ar_SA', hi: 'hi_IN', pl: 'pl_PL', sv: 'sv_SE',
};
const rawLocale = Astro.currentLocale || 'en_US';
const locale = rawLocale.includes('_') || rawLocale.includes('-')
  ? rawLocale.replace('-', '_')
  : (localeMap[rawLocale] || `${rawLocale}_${rawLocale.toUpperCase()}`);

// Determine OG image MIME type from file extension
const extToMime: Record<string, string> = {
  '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg',
  '.webp': 'image/webp', '.gif': 'image/gif', '.svg': 'image/svg+xml',
};
function getImageType(url: string): string | undefined {
  const ext = url.match(/(\.\w+)(?:\?|$)/)?.[1]?.toLowerCase();
  return ext ? extToMime[ext] : undefined;
}
const ogImageType = getImageType(ogImage);
---

<!-- Primary Meta Tags -->
<title>{pageTitle}</title>
<meta name="description" content={description} />
<link rel="canonical" href={canonicalURL.toString()} />
<meta name="robots" content={robotsContent} />

<!-- Open Graph -->
<meta property="og:title" content={pageTitle} />
<meta property="og:type" content={article ? 'article' : 'website'} />
<meta property="og:image" content={ogImage} />
<meta property="og:url" content={canonicalURL.toString()} />
<meta property="og:description" content={description} />
<meta property="og:site_name" content={siteConfig.name} />
<meta property="og:locale" content={locale} />
<meta property="og:image:alt" content={imageAlt || pageTitle} />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
{ogImageType && <meta property="og:image:type" content={ogImageType} />}

<!-- Article Metadata -->
{article?.publishedTime && <meta property="article:published_time" content={article.publishedTime.toISOString()} />}
{article?.modifiedTime && <meta property="article:modified_time" content={article.modifiedTime.toISOString()} />}
{article?.authors?.map((author) => <meta property="article:author" content={author} />)}
{article?.tags?.map((tag) => <meta property="article:tag" content={tag} />)}

<!-- Twitter Card -->
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content={pageTitle} />
<meta name="twitter:description" content={description} />
<meta name="twitter:image" content={ogImage} />
{siteConfig.twitter?.site && <meta name="twitter:site" content={siteConfig.twitter.site} />}
{siteConfig.twitter?.creator && <meta name="twitter:creator" content={siteConfig.twitter.creator} />}

<!-- Verification -->
{siteConfig.verification?.google && <meta name="google-site-verification" content={siteConfig.verification.google} />}
{siteConfig.verification?.bing && <meta name="msvalidate.01" content={siteConfig.verification.bing} />}
