/**
 * ByteLyst MCP Server — port 4007
 *
 * Exposes tool namespaces:
 *   platform.telemetry.*         — query events, clusters, metrics, policy CRUD
 *   platform.diagnostics.*       — manage debug sessions, read logs/traces, cancel
 *   extraction.*                 — run extraction, list models, cache stats, sidecar health
 *   support.*                    — compound tools (createDebugPack, runIncidentPipeline)
 *   mindlyst.*                   — memory, brains, briefs, streaks, reflections, extractions
 *   lysnrai.*                    — transcripts, sessions, orgs, apiTokens, stt
 *   jarvis.*                     — agents, sessions, memory (JarvisJr coaching platform)
 *   chronomind.*                 — timers, routines, syncStatus
 *   nomgap.*                     — fasting sessions, push triggers
 *   notelett.*                   — notes, workspaces, tasks, artifacts, summarize
 *   peakpulse.*                  — adventure sessions, GPS routes, stats
 *   notes.*                      — notes, search, draft creation
 *   tracker.*                    — items, votes, comments, public roadmap
 *   flags.*                      — feature flag CRUD + kill switch
 *   jobs.*                       — background job list, trigger, run history
 *   maintenance.*                — maintenance mode + scheduled windows
 *   settings.*                   — user settings + kill switch check
 *   webhooks.*                   — subscription CRUD, deliveries, test, rotate secret
 *
 * Auth: JWT Bearer tokens issued by platform-service (same JWT_SECRET).
 * Role gating: viewer / admin / super_admin per tool.
 */

import { createServiceApp, registerOptionalJwtContext, startService } from '@bytelyst/fastify-core';
import { config } from './lib/config.js';
import { JwtPayload, verifyJwtToken } from './lib/auth.js';
import { toolRoutes } from './modules/tools/routes.js';

// Register all tool namespaces (side-effect: populates the tool registry)
import './modules/platform/telemetry-tools.js';
import './modules/platform/telemetry-policy-tools.js';
import './modules/platform/diagnostics-tools.js';
import './modules/extraction/extraction-tools.js';
import './modules/support/debug-pack.js';
import './modules/a2a/pipeline-tool.js';
import './modules/a2a/brain-overflow-pipeline.js';
import './modules/a2a/calendar-import-pipeline.js';
import './modules/a2a/daily-brief-pipeline.js';
import './modules/a2a/engagement-pipeline.js';
import './modules/a2a/goal-coaching-pipeline.js';
import './modules/a2a/keyboard-diagnostics-pipeline.js';
import './modules/a2a/marketplace-cert-pipeline.js';
import './modules/a2a/memory-curation-pipeline.js';
import './modules/a2a/nl-parser-eval-pipeline.js';
import './modules/a2a/org-provisioning-pipeline.js';
import './modules/a2a/pipeline-tool.js';
import './modules/a2a/post-incident-cleanup-pipeline.js';
import './modules/a2a/progress-analyst-pipeline.js';
import './modules/a2a/protocol-tuning-pipeline.js';
import './modules/a2a/reflection-synthesis-pipeline.js';
import './modules/a2a/regression-watch-pipeline.js';
import './modules/a2a/route-safety-pipeline.js';
import './modules/a2a/routine-quality-pipeline.js';
import './modules/a2a/safety-monitor-pipeline.js';
import './modules/a2a/ski-run-analyst-pipeline.js';
import './modules/a2a/social-fast-coordinator-pipeline.js';
import './modules/a2a/sync-diagnostics-pipeline.js';
import './modules/a2a/sync-conflict-pipeline.js';
import './modules/a2a/transcript-extraction-pipeline.js';
import './modules/a2a/triage-quality-pipeline.js';
import './modules/a2a/stt-fallback-monitor-pipeline.js';
import './modules/a2a/a2a-tools.js';
import './modules/lysnrai/lysnrai-tools.js';
import './modules/jarvis/jarvis-tools.js';
import './modules/chronomind/chronomind-tools.js';
import './modules/nomgap/nomgap-tools.js';
import './modules/notelett/notelett-tools.js';
import './modules/peakpulse/peakpulse-tools.js';
import './modules/notes/notes-tools.js';
import './modules/tracker/tracker-tools.js';
import './modules/platform/ops-tools.js';
import './modules/platform/webhooks-tools.js';
import './modules/platform/sdk-tools.js';
import './modules/platform/secrets-tools.js';
import './modules/platform/experiments-tools.js';
import './modules/dev/data-generator-tools.js';
import './modules/dev/changelog-tools.js';

const app = await createServiceApp({
  name: 'mcp-server',
  version: '0.1.0',
  description:
    'ByteLyst MCP Server — platform.*, extraction.*, support.*, mindlyst.*, lysnrai.*, jarvis.*, chronomind.*, nomgap.*, peakpulse.*, notes.*, tracker.*, flags.*, jobs.*, maintenance.*, settings.*, webhooks.*',
  corsOrigin: config.CORS_ORIGIN,
  logLevel: config.LOG_LEVEL,
});

const jwtSecret = new TextEncoder().encode(config.JWT_SECRET);

await registerOptionalJwtContext(app, {
  verifyToken: async (token: string) => verifyJwtToken(token, jwtSecret) as Promise<JwtPayload>,
});

// Register tool routes
await app.register(toolRoutes, { prefix: '/api' });

await startService(app, { port: config.PORT, host: config.HOST });
