import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node: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 getSessionsService() {
  if (!service) throw new PlatformError('Sitzungs-Modul ist noch nicht gestartet.', { code: 'SESSIONS_MODULE_NOT_READY', status: 503, expose: true });
  return service;
}

export function createSessionsModule() {
  return {
    manifest,
    lifecycle: {
      async start() { service = (await import('./service.mjs')).createSessionsService(); },
      async stop() { service = null; },
      async health() { return getSessionsService().health(); },
    },
  };
}
