// theme.jsx — palettes, liturgical seasons, type pairings, density

const PALETTES = {
  parchment: {
    name: 'Parchment',
    bg: '#f4ede0',
    bgAlt: '#e8dcc4',
    paper: '#faf5e8',
    ink: '#1a1714',
    inkSoft: '#4a3f2e',
    inkMute: '#8a7a5e',
    rule: 'rgba(26,23,20,0.18)',
    ruleSoft: 'rgba(26,23,20,0.08)',
    accent: '#7a2e2e', // oxblood
    gold: '#b8893d',
    chrome: 'light',
  },
  candlelight: {
    name: 'Candlelight',
    bg: '#1a1410',
    bgAlt: '#251c15',
    paper: '#221a13',
    ink: '#f4e8d2',
    inkSoft: '#d4c5a8',
    inkMute: '#8a7a5e',
    rule: 'rgba(244,232,210,0.18)',
    ruleSoft: 'rgba(244,232,210,0.08)',
    accent: '#d4a85a',
    gold: '#e8b85e',
    chrome: 'dark',
  },
  sepia: {
    name: 'Sepia',
    bg: '#e8d8b8',
    bgAlt: '#d8c498',
    paper: '#efe0c2',
    ink: '#3a2818',
    inkSoft: '#5a3f28',
    inkMute: '#8a6e4e',
    rule: 'rgba(58,40,24,0.22)',
    ruleSoft: 'rgba(58,40,24,0.10)',
    accent: '#6e2818',
    gold: '#a87838',
    chrome: 'light',
  },
};

const SEASONS = {
  ordinary: { name: 'Ordinary Time', accent: '#3a5a3a', label: 'After Pentecost', color: 'green' },
  advent:   { name: 'Advent',         accent: '#3a2858', label: 'Advent',          color: 'purple' },
  christmas:{ name: 'Christmastide',  accent: '#b8893d', label: 'Christmastide',   color: 'gold' },
  epiphany: { name: 'Epiphany',       accent: '#3a5a7a', label: 'Epiphany',        color: 'blue' },
  lent:     { name: 'Lent',           accent: '#4a3858', label: 'Lent',            color: 'violet' },
  easter:   { name: 'Eastertide',     accent: '#a87838', label: 'Eastertide',      color: 'gold' },
  pentecost:{ name: 'Pentecost',      accent: '#7a2e2e', label: 'Pentecost',       color: 'red' },
};

const TYPE_PAIRS = {
  classic:    { display: "'Cormorant Garamond', Georgia, serif", body: "'Newsreader', Georgia, serif", mono: "'JetBrains Mono', monospace", name: 'Cormorant + Newsreader' },
  ebgaramond: { display: "'EB Garamond', Georgia, serif", body: "'EB Garamond', Georgia, serif", mono: "'JetBrains Mono', monospace", name: 'EB Garamond' },
  fraunces:   { display: "'Fraunces', Georgia, serif", body: "'Newsreader', Georgia, serif", mono: "'JetBrains Mono', monospace", name: 'Fraunces + Newsreader' },
  modern:     { display: "'Fraunces', Georgia, serif", body: "'Inter', system-ui, sans-serif", mono: "'JetBrains Mono', monospace", name: 'Fraunces + Inter' },
};

// Resolve a palette name to a concrete entry. 'auto' switches between light
// (parchment) and dark (candlelight) based on the OS-level prefers-color-scheme
// setting — which itself respects the user's "Auto-dark at sunset" toggle on
// iOS/Android/macOS, so we get time-based behavior for free when the user has
// configured it that way at the system level.
function resolvePaletteName(name) {
  if (name !== 'auto') return name;
  if (typeof window !== 'undefined' && window.matchMedia &&
      window.matchMedia('(prefers-color-scheme: dark)').matches) {
    return 'candlelight';
  }
  return 'parchment';
}

function buildTheme(t) {
  const palette = PALETTES[resolvePaletteName(t.palette)] || PALETTES.parchment;
  const season = SEASONS[t.season] || SEASONS.ordinary;
  const pair = TYPE_PAIRS[t.typePair] || TYPE_PAIRS.classic;
  const accent = t.useSeasonAccent ? season.accent : palette.accent;
  const density = t.density || 'spacious';
  const space = density === 'compact' ? { xs: 4, sm: 8, md: 14, lg: 20, xl: 32 } : { xs: 6, sm: 12, md: 20, lg: 32, xl: 48 };

  return {
    ...palette,
    accent,
    season,
    palette: t.palette,
    fonts: pair,
    density,
    space,
    nav: t.nav || 'tower',
    hero: t.hero || 'manuscript',
    paletteName: palette.name,
  };
}

// Small caps label style
function smallCaps(theme, size = 10) {
  return {
    fontFamily: theme.fonts.mono,
    fontSize: size,
    letterSpacing: '0.18em',
    textTransform: 'uppercase',
    fontWeight: 400,
  };
}

Object.assign(window, { PALETTES, SEASONS, TYPE_PAIRS, buildTheme, smallCaps, resolvePaletteName });
