/*
  Content loader. Imports the typed data modules, validates each against its zod
  schema at build (an invalid entry fails the build — DATA_MODEL §8), and exposes
  locale-resolved view models so components never deal with {en,no} pairs directly.
*/

import * as S from './schema';
import { profile as profileData } from '@/data/profile';
import { skills as skillsData } from '@/data/skills';
import { experience as experienceData } from '@/data/experience';
import { meta as metaData } from '@/data/meta';
import { jobtrack } from '@/data/projects/jobtrack';
import { inboxintel } from '@/data/projects/inboxintel';
import { homelab } from '@/data/projects/homelab';
import type { Locale } from '@i18n/locales';
import { pathFor, type PageId, type ProjectPageId } from '@i18n/slugMap';

// ---- Validate at module load (build-time gate) ----
const PROFILE = S.profile.parse(profileData);
const SKILLS = S.skills.parse(skillsData);
const EXPERIENCE = S.experience.parse(experienceData);
const META = S.meta.parse(metaData);
const PROJECTS: S.Project[] = [jobtrack, inboxintel, homelab]
  .map((p) => S.project.parse(p))
  .sort((a, b) => a.order - b.order);

/** Pick the locale variant of a localised pair. */
function pick<T>(pair: { en: T; no: T }, locale: Locale): T {
  return pair[locale];
}

// ---------------- Profile ----------------
export function getProfile(locale: Locale) {
  const c = PROFILE.content[locale];
  return {
    name: PROFILE.name,
    links: PROFILE.links,
    cv: PROFILE.cv,
    cvCurrent: PROFILE.cv[locale],
    photo: {
      src: PROFILE.photo.src,
      alt: pick(PROFILE.photo.alt, locale),
      caption: pick(PROFILE.photo.caption, locale),
    },
    languages: PROFILE.languages.map((l) => ({
      label: pick(l.label, locale),
      level: pick(l.level, locale),
    })),
    chips: PROFILE.chips.map((ch) => ({ id: ch.id, label: pick(ch.label, locale) })),
    proof: PROFILE.proof.map((p) => ({ stat: pick(p.stat, locale), label: pick(p.label, locale) })),
    ...c,
  };
}
export type ProfileVM = ReturnType<typeof getProfile>;

// ---------------- Skills ----------------
export function getSkills(locale: Locale) {
  return SKILLS.groups.map((g) => ({
    id: g.id,
    title: pick(g.title, locale),
    context: pick(g.context, locale),
    skills: g.skills.map((s) => ({
      name: s.name,
      tooltip: s.tooltip ? pick(s.tooltip, locale) : undefined,
    })),
  }));
}

// ---------------- Experience ----------------
export function getExperience(locale: Locale) {
  return EXPERIENCE.items.map((it) => ({
    id: it.id,
    employer: it.employer,
    location: it.location,
    period: it.period,
    emphasis: it.emphasis,
    alongside: it.alongside,
    progression: it.progression?.map((p) => ({
      from: p.from,
      to: p.to,
      label: pick(p.label, locale),
    })),
    role: pick(it.role, locale),
    summary: it.summary ? pick(it.summary, locale) : undefined,
    highlights: it.highlights ? pick(it.highlights, locale) : undefined,
  }));
}

// ---------------- Projects ----------------
function projectCard(p: S.Project, locale: Locale) {
  const c = p.content[locale];
  return {
    id: p.id as ProjectPageId,
    name: p.name,
    status: p.status,
    order: p.order,
    template: p.template,
    path: pathFor(p.id as PageId, locale),
    stack: p.stack.map((s) => ({
      name: s.name,
      context: s.context ? pick(s.context, locale) : undefined,
    })),
    valueProp: c.valueProp,
    cardTeaser: c.cardTeaser,
    heroMedia: p.media[0]
      ? {
          src: p.media[0].src,
          width: p.media[0].width,
          height: p.media[0].height,
          alt: pick(p.media[0].alt, locale),
          caption: pick(p.media[0].caption, locale),
        }
      : undefined,
  };
}

export function getProjectCards(locale: Locale) {
  return PROJECTS.map((p) => projectCard(p, locale));
}

export function getProject(id: ProjectPageId, locale: Locale) {
  const p = PROJECTS.find((x) => x.id === id);
  if (!p) throw new Error(`Unknown project: ${id}`);
  const c = p.content[locale];
  return {
    ...projectCard(p, locale),
    links: p.links,
    diagram: p.diagram
      ? {
          viewBox: p.diagram.viewBox,
          title: pick(p.diagram.title, locale),
          desc: pick(p.diagram.desc, locale),
          nodes: p.diagram.nodes.map((n) => ({
            ...n,
            sub: n.sub ? pick(n.sub, locale) : undefined,
          })),
          edges: p.diagram.edges,
        }
      : undefined,
    media: p.media.map((m) => ({
      src: m.src,
      width: m.width,
      height: m.height,
      alt: pick(m.alt, locale),
      caption: pick(m.caption, locale),
    })),
    tldr: c.tldr,
    sections: c.sections,
  };
}
export type ProjectVM = ReturnType<typeof getProject>;

export function getProjectIds(): ProjectPageId[] {
  return PROJECTS.map((p) => p.id as ProjectPageId);
}

// ---------------- Meta ----------------
export function getMeta(pageId: PageId, locale: Locale): { title: string; description: string } {
  const m = META[pageId];
  if (!m) throw new Error(`No page meta for: ${pageId}`);
  return m[locale];
}
