import { readFileSync } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { createAuthModule } from './auth/index.mjs';
import { createSecurityModule } from './security/index.mjs';
import { createSessionsModule } from './sessions/index.mjs';
import { createSettingsModule } from './settings/index.mjs';
import { createListingsModule } from './listings/index.mjs';
import { createInventoryModule } from './inventory/index.mjs';
import { createMediaModule } from './media/index.mjs';
import { createThemesModule } from './themes/index.mjs';
import { createUsersModule } from './users/index.mjs';
import { createModuleManagerModule } from './module-manager/index.mjs';
import { createAiModule } from './ai/index.mjs';
import { createFluxStudioModule } from './flux-studio/index.mjs';
import { createQualityModule } from './quality/index.mjs';
import { createPublishingModule } from './publishing/index.mjs';
import { createDeploymentsModule } from './deployments/index.mjs';

const here = dirname(fileURLToPath(import.meta.url));
const legacyModuleNames = ['system', 'operations'];

function legacyModule(name) {
  return {
    manifest: JSON.parse(readFileSync(join(here, name, 'module.json'), 'utf8')),
    lifecycle: {
      async start() {}, async stop() {}, async health() { return { ok: true, bridge: 'legacy' }; },
    },
  };
}

export function loadBuiltInModules() {
  return [
    ...legacyModuleNames.map(legacyModule),
    createSecurityModule(), createAuthModule(), createSessionsModule(), createUsersModule(), createSettingsModule(), createThemesModule(),
    createMediaModule(), createListingsModule(), createInventoryModule(), createAiModule(), createFluxStudioModule(), createQualityModule(), createPublishingModule(), createModuleManagerModule(), createDeploymentsModule(),
  ];
}
