import { readFileSync } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { PlatformError } from '../../kernel/errors.mjs';

const here = dirname(fileURLToPath(import.meta.url));
const manifest = JSON.parse(readFileSync(join(here, 'module.json'), 'utf8'));
let service = null;

export function getThemeService() {
  if (!service) throw new PlatformError('Theme-Modul ist noch nicht gestartet.', { code: 'THEME_MODULE_NOT_READY', status: 503, expose: true });
  return service;
}

export function createThemesModule() {
  return {
    manifest,
    lifecycle: {
      async start() {
        const { createThemeService } = await import('./service.mjs');
        service = createThemeService();
      },
      async stop() { service = null; },
      async health() { return getThemeService().health(); },
    },
  };
}
