// CMSRuntime.jsx — PROYECTO Public Content Runtime
//
// This file ships to every visitor and contains:
//   - DEFAULT_CONTENT: hardcoded fallback copy/specs for every page
//   - mergeWithDefaults: helper used by the store loader
//   - window.__PROYECTO_CMS_STORE: the in-memory content store, backed by localStorage
//   - useCMSContent: React hook that public pages use to read content
//
// Public pages (LandingPage, StudioPage, ContactPage, EventsGate) and
// index.html's App boot logic depend on the three globals exported here:
//   - window.__PROYECTO_CMS_STORE
//   - window.useCMSContent
//   - window.DEFAULT_CMS_CONTENT (alias of DEFAULT_CONTENT)

const DEFAULT_CONTENT = {
  studios: {
    studio01: {
      num: '01',
      name: 'PREMIUM',
      titleLine1: '01 — PRE-VIZ',
      titleLine2: 'PREMIUM',
      line1Color: '#ffffff',
      line2Color: 'var(--accent, #e6351e)',
      tagline: 'The highest quality pre-viz infrastructure.',
      description: 'Purpose-built for lighting designers running Depence, MA3, and related pre-viz software. Used by LD teams working for artists at the highest level of touring production.',
      overviewLabel: 'OVERVIEW', overviewLabelColor: 'var(--accent, #e6351e)',
      accessLabel: 'ACCESS', accessLabelColor: 'var(--accent, #e6351e)',
      requestAccessText: 'REQUEST ACCESS', requestAccessColor: '#ffffff',
      inquireBtnText: 'INQUIRE ▸', inquireBtnColor: 'var(--accent, #e6351e)',
      specsLabel: 'SPECIFICATIONS', specsLabelColor: 'var(--accent, #e6351e)',
      image: '',
      specs: [
        ['SOFTWARE',      'DEPENCE / MA3 / WYSIWYG'],
        ['NETWORKING',    'ArtNET / sACN / NDI'],
        ['POWER',         '200A DISTRO'],
        ['BLACKOUT',      'FULLY BLACKOUT CAPABLE'],
        ['WORKSTATIONS',  'HIGH-SPEC RENDERING RIGS'],
        ['ACCESS',        'BY APPOINTMENT'],
        ['AVAILABILITY',  'DAILY / WEEKLY'],
      ],
    },
    studio02: {
      num: '02',
      name: 'STANDARD',
      titleLine1: '02 — PRE-VIZ',
      titleLine2: 'STANDARD',
      line1Color: '#ffffff',
      line2Color: 'var(--accent, #e6351e)',
      tagline: 'Professional-grade pre-visualization at a lower price point.',
      description: 'MA3 compatible, blackout capable, networked. Built for LDs who need a serious working environment while looking to save costs.',
      overviewLabel: 'OVERVIEW', overviewLabelColor: 'var(--accent, #e6351e)',
      accessLabel: 'ACCESS', accessLabelColor: 'var(--accent, #e6351e)',
      requestAccessText: 'REQUEST ACCESS', requestAccessColor: '#ffffff',
      inquireBtnText: 'INQUIRE ▸', inquireBtnColor: 'var(--accent, #e6351e)',
      specsLabel: 'SPECIFICATIONS', specsLabelColor: 'var(--accent, #e6351e)',
      image: '',
      specs: [
        ['SOFTWARE',      'MA3 / DEPENCE COMPATIBLE'],
        ['NETWORKING',    'ArtNET / sACN'],
        ['POWER',         '100A DISTRO'],
        ['BLACKOUT',      'FULLY BLACKOUT CAPABLE'],
        ['WORKSTATIONS',  'STANDARD RENDERING SETUP'],
        ['ACCESS',        'BY APPOINTMENT'],
        ['AVAILABILITY',  'DAILY / WEEKLY'],
      ],
    },
    studio03: {
      num: '03',
      name: 'REHEARSAL /EVENT',
      titleLine1: '03 —',
      titleLine2: 'REHEARSAL /EVENT',
      line1Color: '#ffffff',
      line2Color: 'var(--accent, #e6351e)',
      tagline: 'Built for artists and brands operating at a high level.',
      description: 'A rehearsal space for touring artists, brands, and production teams. Designed by people who have built and toured shows at scale. That expertise is embedded in the space.',
      overviewLabel: 'OVERVIEW', overviewLabelColor: 'var(--accent, #e6351e)',
      accessLabel: 'ACCESS', accessLabelColor: 'var(--accent, #e6351e)',
      requestAccessText: 'REQUEST ACCESS', requestAccessColor: '#ffffff',
      inquireBtnText: 'INQUIRE ▸', inquireBtnColor: 'var(--accent, #e6351e)',
      specsLabel: 'SPECIFICATIONS', specsLabelColor: 'var(--accent, #e6351e)',
      image: '',
      specs: [
        ['SQUARE FOOTAGE', '6,000 SQ FT'],
        ['CEILING HEIGHT', '24 FT'],
        ['CDJS',           'PIONEER CDJ-3000X'],
        ['AUDIO',          'Pioneer DJ VM-70 SPEAKERS'],
        ['VISUAL',         'HIGH-FIDELITY MONITORS'],
        ['ACCESS',         'BY APPOINTMENT'],
        ['AVAILABILITY',   'DAILY / WEEKLY'],
      ],
    },
  },
  contact: {
    email: 'BOOKINGS@PROYECTO.LA',
    emailColor: 'var(--accent, #e6351e)',
    subtitle: 'PRIVATE FACILITY · BY APPOINTMENT ONLY',
  },
  landing: {
    title: 'PROYECTO',
    titleColor: '#ffffff',
    aboutLabel: 'ABOUT',
    aboutLabelColor: 'var(--accent, #e6351e)',
    about: 'A private production facility in Los Angeles. Pre-viz suites, rehearsal studios, and event space purpose-built for touring artists and the teams who design their shows and experiences.',
    location: 'LOS ANGELES · PRIVATE FACILITY',
  },
  terminal: {
    subtitle: 'Built from the stage out.',
  },
  theme: {
    accent:           '#e6351e',
    welcomeFlash:     '#e6351e',
    welcomeFlashSoft: '#8A9099',
    highFlash:        '#00ffd1',
    highFlashSoft:    '#7af5d8',
    eventsAccent:     '#e6351e',
    eventsFlash:      '#e6351e',
    eventsFlashSoft:  '#f29585',
  },
  events: {
    list: [
      {
        id: 'evt_001',
        title: 'isoXO',
        subtitle: '',
        promoter: 'HCD',
        date: '2026-05-08',
        time: '8:00 PM',
        image: '',
        ticketUrl: 'https://example.com/tickets',
        active: true,
      },
    ],
  },
  global: {
    footerYear: '2025',
  },
  grantText: {
    // Terminal — low clearance ('granted-low')
    terminalLowHeading:    'ACCESS GRANTED',

    // Terminal — high clearance ('granted-high')
    terminalHighHeading:   'CLEARANCE ELEVATED',
    terminalHighSubtitle:  'level 02 — proyecto',
    terminalHighTicker1:   'AUTH OK',
    terminalHighTicker2:   'VAULT UNLOCKED',
    terminalHighTicker3:   'FULL ACCESS',

    // Events gate — granting flash before list reveal
    eventsGrantHeading:    '04 ACCESS GRANTED',
    eventsGrantSubtitle:   'events // proyecto',
    eventsGrantTicker1:    'CODE OK',
    eventsGrantTicker2:    'EVENTS QUEUE OPEN',
    eventsGrantTicker3:    'PROCEED',
  },
};


// ── Content store — reads/writes to localStorage ────────────────────────
const mergeWithDefaults = (defaults, loaded) => {
  const result = JSON.parse(JSON.stringify(defaults));
  for (const key of Object.keys(loaded)) {
    if (
      loaded[key] !== null &&
      typeof loaded[key] === 'object' &&
      !Array.isArray(loaded[key]) &&
      result[key] &&
      typeof result[key] === 'object' &&
      !Array.isArray(result[key])
    ) {
      result[key] = mergeWithDefaults(result[key], loaded[key]);
    } else {
      result[key] = loaded[key];
    }
  }
  return result;
};

window.__PROYECTO_CMS_STORE = window.__PROYECTO_CMS_STORE || (() => {
  const load = () => {
    try {
      const saved = JSON.parse(localStorage.getItem('proyecto_cms') || 'null');
      if (!saved) return JSON.parse(JSON.stringify(DEFAULT_CONTENT));
      return mergeWithDefaults(JSON.parse(JSON.stringify(DEFAULT_CONTENT)), saved);
    } catch(e) { return JSON.parse(JSON.stringify(DEFAULT_CONTENT)); }
  };
  let state = load();
  const listeners = new Set();
  return {
    get: () => state,
    set: (next) => {
      state = next;
      localStorage.setItem('proyecto_cms', JSON.stringify(state));
      listeners.forEach(l => l(state));
    },
    reset: () => {
      state = JSON.parse(JSON.stringify(DEFAULT_CONTENT));
      localStorage.setItem('proyecto_cms', JSON.stringify(state));
      listeners.forEach(l => l(state));
    },
    subscribe: (fn) => { listeners.add(fn); return () => listeners.delete(fn); },
  };
})();

const useCMSContent = () => {
  const [, force] = React.useState(0);
  React.useEffect(() => {
    return window.__PROYECTO_CMS_STORE.subscribe(() => force(n => n + 1));
  }, []);
  return window.__PROYECTO_CMS_STORE.get();
};

// Expose the public globals.
Object.assign(window, { useCMSContent, DEFAULT_CMS_CONTENT: DEFAULT_CONTENT });
