/**
 * Commands MCP Server - Development
 *
 * Provides development server management operations.
 * Token savings: 3,000+ per operation
 */

import { callMCPTool } from "../../client.js";

export interface DevServerResult {
  message: string;
  port: number;
  url: string;
}

export interface BuildResult {
  success: boolean;
  output: string;
  duration?: number;
  errors?: string[];
}

export interface LintResult {
  success: boolean;
  output: string;
  errors: number;
  warnings: number;
  fixable: number;
}

/**
 * Start development server on port 9000.
 *
 * @returns Server start confirmation
 *
 * @example
 * ```typescript
 * const server = await startDevServer();
 * console.log(server.message); // "Dev server starting on port 9000..."
 * console.log(`Visit: ${server.url}`);
 * ```
 */
export async function startDevServer(): Promise<DevServerResult> {
  return callMCPTool<DevServerResult>("commands__run_dev", {});
}

/**
 * Build the project for production.
 *
 * @returns Build result with output
 *
 * @example
 * ```typescript
 * const result = await buildProject();
 *
 * if (result.success) {
 *   console.log('✓ Build successful');
 *   // Only show last 20 lines of output
 *   const lines = result.output.split('\n');
 *   console.log(lines.slice(-20).join('\n'));
 * } else {
 *   console.log('✗ Build failed');
 *   result.errors?.forEach(err => console.log(`  - ${err}`));
 * }
 * ```
 */
export async function buildProject(): Promise<BuildResult> {
  return callMCPTool<BuildResult>("commands__run_build", {});
}

/**
 * Run ESLint on the project.
 *
 * @returns Lint results
 *
 * @example
 * ```typescript
 * const lint = await runLint();
 *
 * console.log(`Errors: ${lint.errors}, Warnings: ${lint.warnings}`);
 * console.log(`Fixable: ${lint.fixable}`);
 *
 * if (!lint.success && lint.errors > 0) {
 *   // Show first 10 lines of errors
 *   console.log(lint.output.split('\n').slice(0, 10).join('\n'));
 * }
 * ```
 */
export async function runLint(): Promise<LintResult> {
  return callMCPTool<LintResult>("commands__run_lint", {});
}

/**
 * Check if dev server is running.
 *
 * @returns Server status
 *
 * @example
 * ```typescript
 * const status = await checkDevServer();
 * console.log(status.running ? '✓ Server running' : '✗ Server not running');
 * ```
 */
export async function checkDevServer(): Promise<{
  running: boolean;
  port?: number;
}> {
  return callMCPTool("commands__check_dev_server", {});
}

export default {
  startDevServer,
  buildProject,
  runLint,
  checkDevServer,
};
