/**
 * pres/themes — theme registry + resolver.
 *
 * The agent passes `theme: 'editorial-warm'` (or one of the other
 * presets). This module turns that string + optional per-axis overrides
 * (palette, spacing, shape, background) into a fully-resolved Theme
 * object that downstream archetypes consume.
 *
 * Adding a theme:
 *   1. Create `themes/<id>.js` exporting the shape these three follow
 *   2. Register it below in THEMES
 *   3. Pick a default palette + defaults for the other 4 axes
 */
import editorialWarm from './editorial-warm.js';
import techBold from './tech-bold.js';
import dataHeavy from './data-heavy.js';
import monoKeynote from './mono-keynote.js';

import { resolvePalette } from '../palettes.js';
import { resolveSpacing } from '../grid.js';
import { resolveShape } from '../shapes.js';

const THEMES = {
  'editorial-warm': editorialWarm,
  'tech-bold':      techBold,
  'data-heavy':     dataHeavy,
  'mono-keynote':   monoKeynote,
};

/**
 * List the available theme presets — used by the showcase verifier
 * and by the agent-facing tool description.
 */
export function listThemes() {
  return Object.values(THEMES).map(t => ({
    id: t.id,
    family: t.family,
    description: t.description,
    defaults: t.defaults,
  }));
}

/**
 * Resolve a theme spec into the full runtime object every archetype
 * + background generator consumes.
 *
 * Inputs:
 *   spec.theme       — 'editorial-warm' | 'tech-bold' | 'data-heavy'
 *   spec.palette     — optional palette override
 *   spec.spacing     — optional spacing override
 *   spec.shape       — optional shape override
 *   spec.background  — optional background-style override (theme-wide)
 *
 * The returned object holds:
 *   theme            — the registered theme module (typography, archetypes config)
 *   palette          — resolved palette object (6 semantic roles)
 *   spacing          — resolved spacing metrics (margins, grid, body/header regions)
 *   shape            — resolved shape tokens (corner radius, shadow)
 *   bg               — resolved background style id (used by backgrounds/index.js)
 *   decoration       — passthrough decoration config from the theme module
 */
export function resolveTheme(spec = {}) {
  const id = spec.theme && THEMES[spec.theme] ? spec.theme : 'editorial-warm';
  const theme = THEMES[id];

  const palette = resolvePalette(spec.palette || theme.defaults.palette, theme.family);
  const spacing = resolveSpacing(spec.spacing || theme.defaults.spacing);
  const shape   = resolveShape(spec.shape   || theme.defaults.shape);
  const bg      = spec.background || theme.defaults.background;

  return {
    theme,
    palette,
    spacing,
    shape,
    bg,
    decoration: theme.decoration || {},
    typography: theme.typography,
    /** Convenience accessor: theme-specific archetype config. */
    archetypeConfig(archetypeId) {
      return (theme.archetypes && theme.archetypes[archetypeId]) || {};
    },
  };
}
