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 getUsersService() {
  if (!service) throw new PlatformError('Benutzer-Modul ist noch nicht gestartet.', { code: 'USERS_MODULE_NOT_READY', status: 503, expose: true });
  return service;
}

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