/*
  The EN↔NO route contract (ROUTING_SPEC §1, ARCHITECTURE A5).
  Single source of truth consumed by: the language switch, hreflang generation,
  the sitemap, nav links, and breadcrumbs. CI asserts its bijection (see tests).
*/

import { LOCALES, DEFAULT_LOCALE, type Locale } from './locales';

export const PAGE_IDS = [
  'home',
  'projects',
  'jobtrack',
  'inboxintel',
  'homelab',
  'experience',
  'about',
  'contact',
  'cv',
  'colophon',
] as const;

export type PageId = (typeof PAGE_IDS)[number];

/** Canonical, trailing-slashed paths per page per locale. */
export const ROUTES: Record<PageId, Record<Locale, string>> = {
  home: { en: '/', no: '/no/' },
  projects: { en: '/projects/', no: '/no/prosjekter/' },
  jobtrack: { en: '/projects/jobtrack/', no: '/no/prosjekter/jobtrack/' },
  inboxintel: { en: '/projects/inboxintel/', no: '/no/prosjekter/inboxintel/' },
  homelab: { en: '/projects/homelab/', no: '/no/prosjekter/hjemmelab/' },
  experience: { en: '/experience/', no: '/no/erfaring/' },
  about: { en: '/about/', no: '/no/om-meg/' },
  contact: { en: '/contact/', no: '/no/kontakt/' },
  cv: { en: '/cv/', no: '/no/cv/' },
  colophon: { en: '/colophon/', no: '/no/kolofon/' },
};

/** Which case-study/capability pages appear as project cards, in order. */
export const PROJECT_PAGE_IDS = ['jobtrack', 'inboxintel', 'homelab'] as const;
export type ProjectPageId = (typeof PROJECT_PAGE_IDS)[number];

/**
 * Homepage section anchors (ROUTING_SPEC §5). `id` is the canonical key used by
 * scroll-spy; the localised fragment is what appears in the URL and nav link.
 */
export const HOME_SECTIONS = [
  { id: 'skills', en: 'skills', no: 'kompetanse' },
  { id: 'projects', en: 'projects', no: 'prosjekter' },
  { id: 'experience', en: 'experience', no: 'erfaring' },
  { id: 'about', en: 'about', no: 'om-meg' },
  { id: 'contact', en: 'contact', no: 'kontakt' },
] as const;

export function homeAnchor(
  sectionId: (typeof HOME_SECTIONS)[number]['id'],
  locale: Locale,
): string {
  const s = HOME_SECTIONS.find((x) => x.id === sectionId);
  return s ? s[locale] : sectionId;
}

/** Absolute-within-site path for a page in a locale. */
export function pathFor(pageId: PageId, locale: Locale): string {
  return ROUTES[pageId][locale];
}

/** Normalise a path to the canonical trailing-slash form for comparison. */
function normalise(path: string): string {
  const clean = path.split('?')[0]!.split('#')[0]!;
  if (clean === '') return '/';
  return clean.endsWith('/') ? clean : `${clean}/`;
}

/** Resolve the pageId that a URL path belongs to (or undefined). */
export function pageIdFromPath(path: string): PageId | undefined {
  const target = normalise(path);
  for (const id of PAGE_IDS) {
    for (const locale of LOCALES) {
      if (ROUTES[id][locale] === target) return id;
    }
  }
  return undefined;
}

/** Infer the locale of a URL path from its prefix. */
export function localeFromPath(path: string): Locale {
  const clean = normalise(path);
  return clean === '/no/' || clean.startsWith('/no/') ? 'no' : DEFAULT_LOCALE;
}

/**
 * Given the current path, return the equivalent page's path in `target` locale.
 * This is the language-switch mapping (ROUTING_SPEC §3, J5). Falls back to the
 * target-locale home if the current path is unmapped (e.g. 404).
 */
export function twinPath(currentPath: string, target: Locale): string {
  const id = pageIdFromPath(currentPath);
  if (!id) return ROUTES.home[target];
  return ROUTES[id][target];
}

/** Every (pageId, locale, path) triple — used by the sitemap builder. */
export function allRoutes(): Array<{ pageId: PageId; locale: Locale; path: string }> {
  const out: Array<{ pageId: PageId; locale: Locale; path: string }> = [];
  for (const pageId of PAGE_IDS) {
    for (const locale of LOCALES) {
      out.push({ pageId, locale, path: ROUTES[pageId][locale] });
    }
  }
  return out;
}
