// Per-site CMS configuration loader.
//
// A site tells the core what it manages through one module, referenced by the
// CMS_CONFIG env var (e.g. CMS_CONFIG=/site/cms.config.js). The module exports
// `default` with { collections, plugins, admins }. Loaded once at startup; the
// engine (files/stats/routes) derives its behaviour from it instead of having
// openbureau-specific content types hard-coded.
//
// See ../../README.md for the schema, and ../../examples/*.config.js.
import { pathToFileURL } from 'node:url';

const p = process.env.CMS_CONFIG;
if (!p) {
  console.error('FEHLT: CMS_CONFIG (Pfad zur Site-Config, z. B. /site/cms.config.js)');
  process.exit(1);
}

const mod = await import(pathToFileURL(p).href);
export const config = mod.default || mod;
config.collections ||= [];
config.plugins ||= [];
config.admins ||= [];
config.auth ||= 'supabase'; // 'supabase' (GoTrue) | 'local' (file users, DB-less)

export const hasPlugin = (name) => config.plugins.includes(name);

// Collection lookup helpers (used by the generalised files.js / stats.js).
export const collectionOf = (kind) => config.collections.find((c) => c.kind === kind) || null;
export const fallbackCollection = () => config.collections.find((c) => c.fallback) || null;
