import { validateModule } from '../params/schema.js';

import { nebula } from './shader/nebula.js';
import { classicWave } from './shader/classic-wave.js';
import { floatingGeometry } from './shader/floating-geometry.js';
import { synthwaveRun } from './shader/synthwave-run.js';
import { psychedelicDrift } from './shader/psychedelic-drift.js';
import { particleField } from './layers3d/particles.js';

/**
 * The scene library. Families exist so the arc driver can choose by section
 * character rather than at random — a breakdown never lands on a strobing glitch
 * scene, and an intro never opens at full density.
 */
export const FAMILIES = {
    flow:       { label: 'Flow',       energy: [0.0, 0.7] },
    organic:    { label: 'Organic',    energy: [0.0, 0.8] },
    minimal:    { label: 'Minimal',    energy: [0.0, 0.45] },
    structural: { label: 'Structural', energy: [0.3, 0.9] },
    geometric:  { label: 'Geometric',  energy: [0.4, 1.0] },
    glitch:     { label: 'Glitch',     energy: [0.6, 1.0] },
};

const MODULES = [
    nebula,
    classicWave,
    floatingGeometry,
    synthwaveRun,
    psychedelicDrift,
    particleField,
];

const errors = [];
for (const m of MODULES) {
    const e = validateModule(m);
    if (e.length) errors.push(...e);
    if (m.family && !FAMILIES[m.family]) errors.push(`${m.name}: unknown family '${m.family}'`);
}
if (errors.length) {
    // Fail loudly at import: a malformed scene must never reach the compositor,
    // where the symptom would be a black frame with no explanation.
    console.error('[registry] invalid scene modules:\n' + errors.join('\n'));
}

export const scenes = MODULES;
export const sceneErrors = errors;

export function sceneByName(name) {
    return MODULES.find((m) => m.name === name) || null;
}

export function scenesInFamily(family) {
    return MODULES.filter((m) => m.family === family);
}

export function familyNames() {
    return Object.keys(FAMILIES);
}

export default scenes;
