import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { config } from './config.js';
import { detectProject, summarizeDetection } from './lib/frameworks/detect.js';
import { registerWorkflowTools } from './tools/workflow/index.js';
import { registerScaffoldTools } from './tools/scaffold/index.js';
import { registerLaravelTools } from './tools/laravel/index.js';
import { registerCodeIgniterTools } from './tools/codeigniter/index.js';
import { registerReactTools } from './tools/react/index.js';
import { registerBrowserTools } from './tools/browser/index.js';
import { registerDockerTools } from './tools/docker/index.js';
import { registerDocsTools } from './tools/docs/index.js';
import { registerResources } from './tools/resources/index.js';
import { registerPrompts } from './tools/prompts/index.js';
import { registerGitTools } from './tools/git/index.js';
import { registerAuditTools } from './tools/audit/index.js';
import { registerBackendResourceTools } from './tools/backends/index.js';

const SERVER_VERSION = '0.6.0';

export function createServer(): McpServer {
  const server = new McpServer(
    { name: 'web-dev-mcp', version: SERVER_VERSION },
    {
      instructions:
        'Tools for web development: framework-aware workflow for Laravel (Sail-aware), CodeIgniter, Symfony, Django, Rails, ' +
        'React, Vue, and Nuxt (with monorepo detect_workspace + package scoping), scaffolding, ' +
        'Laravel/CodeIgniter/React/Next + generate_django/rails/symfony_resource tools, browser_*, docker_*, docs, ' +
        'git_* (incl. git_diff_summarize), lighthouse_audit, resources, and prompts (incl. visual-diff-ci). ' +
        'Call detect_framework or detect_workspace first.',
    },
  );

  registerWorkflowTools(server);
  registerScaffoldTools(server);
  registerLaravelTools(server);
  registerCodeIgniterTools(server);
  registerReactTools(server);
  registerBackendResourceTools(server);
  registerBrowserTools(server);
  registerDockerTools(server);
  registerDocsTools(server);
  registerGitTools(server);
  registerAuditTools(server);
  registerResources(server);
  registerPrompts(server);

  server.registerResource(
    'project-info',
    'workspace://project.json',
    {
      title: 'Project Info',
      description: 'Detected framework adapter(s) and metadata for the current workspace.',
      mimeType: 'application/json',
    },
    async (uri) => {
      const detected = detectProject(config.workspaceRoot);
      return {
        contents: [
          {
            uri: uri.href,
            mimeType: 'application/json',
            text: JSON.stringify(summarizeDetection(detected), null, 2),
          },
        ],
      };
    },
  );

  return server;
}
