import type { Metadata } from 'next';

/**
 * Canonical site origin. NEXT_PUBLIC_SITE_URL wins (it is inlined at build time); otherwise production
 * builds point at the real domain and dev at localhost, so canonical/OG URLs are never mixed up.
 */
function resolveSiteUrl(): URL {
  const fromEnv = process.env.NEXT_PUBLIC_SITE_URL?.trim();
  const fallback = process.env.NODE_ENV === 'production' ? 'https://pintrush.com' : 'http://localhost:3000';
  try {
    return new URL(fromEnv && fromEnv.length > 0 ? fromEnv : fallback);
  } catch {
    return new URL(fallback);
  }
}

export const siteUrl: URL = resolveSiteUrl();

export const site = {
  name: 'PintRush',
  promise: 'The Bar Is the Arena. Everyone Plays.',
  signatureGame: 'The Rush',
  description:
    'PintRush turns the TVs in your bar into a live game show. Guests scan a QR code and play The Rush — live trivia, a 3×3 bingo board, table battles and power-ups — from their phones. No app, no sign-up.',
  shortDescription: 'Live trivia game nights for bars. Guests play on their phones, the TV runs the show.',
  locale: 'en_US',
  region: 'Long Island, NY',
} as const;

export interface NavItem {
  href: string;
  label: string;
}

export const mainNav: readonly NavItem[] = [
  { href: '/how-it-works', label: 'How it works' },
  { href: '/for-venues', label: 'For venues' },
  { href: '/pricing', label: 'Pricing' },
  { href: '/demo', label: 'Demo' },
  { href: '/faq', label: 'FAQ' },
];

export const footerNav: readonly { title: string; items: readonly NavItem[] }[] = [
  {
    title: 'Product',
    items: [
      { href: '/how-it-works', label: 'How it works' },
      { href: '/demo', label: 'Live demo' },
      { href: '/pricing', label: 'Pricing' },
    ],
  },
  {
    title: 'Venues',
    items: [
      { href: '/for-venues', label: 'For venues' },
      { href: '/pilot', label: 'Join the pilot' },
      { href: '/faq', label: 'FAQ' },
    ],
  },
  {
    title: 'Legal',
    items: [
      { href: '/legal/privacy', label: 'Privacy' },
      { href: '/legal/terms', label: 'Terms' },
    ],
  },
];

/** Public, indexable marketing routes (sitemap source of truth). */
export const marketingRoutes: readonly { path: string; priority: number; changeFrequency: 'weekly' | 'monthly' | 'yearly' }[] = [
  { path: '/', priority: 1, changeFrequency: 'weekly' },
  { path: '/how-it-works', priority: 0.9, changeFrequency: 'monthly' },
  { path: '/for-venues', priority: 0.9, changeFrequency: 'monthly' },
  { path: '/pricing', priority: 0.8, changeFrequency: 'monthly' },
  { path: '/pilot', priority: 0.9, changeFrequency: 'monthly' },
  { path: '/demo', priority: 0.7, changeFrequency: 'monthly' },
  { path: '/faq', priority: 0.6, changeFrequency: 'monthly' },
  { path: '/legal/privacy', priority: 0.2, changeFrequency: 'yearly' },
  { path: '/legal/terms', priority: 0.2, changeFrequency: 'yearly' },
];

/** Paths crawlers must not index (app surfaces, APIs). */
export const privatePaths: readonly string[] = ['/host', '/admin', '/play', '/tv', '/join', '/api'];

export function absoluteUrl(path = '/'): string {
  return new URL(path, siteUrl).toString();
}

const socialImageAlt = `${site.name} — ${site.promise}`;

/**
 * Per-page metadata with a canonical URL. The social images (app/opengraph-image.tsx and
 * app/twitter-image.tsx) are referenced explicitly: a page-level `openGraph`/`twitter` object
 * replaces the parent's wholesale, which would otherwise drop the file-based images.
 */
export function pageMetadata({ title, description, path }: { title?: string; description: string; path: string }): Metadata {
  const ogTitle = title ? `${title} · ${site.name}` : socialImageAlt;
  return {
    ...(title ? { title } : {}),
    description,
    alternates: { canonical: path },
    openGraph: {
      type: 'website',
      siteName: site.name,
      locale: site.locale,
      url: path,
      title: ogTitle,
      description,
      images: [{ url: '/opengraph-image', width: 1200, height: 630, alt: socialImageAlt, type: 'image/png' }],
    },
    twitter: {
      card: 'summary_large_image',
      title: ogTitle,
      description,
      images: [{ url: '/twitter-image', width: 1200, height: 630, alt: socialImageAlt }],
    },
  };
}
