/**
 * Testing MCP Server - Coverage
 *
 * Provides test coverage analysis and reporting.
 * Token savings: 10,000+ per operation (large coverage reports)
 */

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

export interface CoverageReport {
  statements: { total: number; covered: number; percent: number };
  branches: { total: number; covered: number; percent: number };
  functions: { total: number; covered: number; percent: number };
  lines: { total: number; covered: number; percent: number };
  files: Array<{
    path: string;
    statements: number;
    branches: number;
    functions: number;
    lines: number;
  }>;
}

export interface CoverageSummary {
  overall: {
    statements: number;
    branches: number;
    functions: number;
    lines: number;
  };
  meetsTarget: boolean; // 70% target
  needsImprovement: string[]; // Files below target
}

/**
 * Get current test coverage report.
 *
 * @returns Detailed coverage report
 *
 * @example
 * ```typescript
 * const coverage = await getCoverageReport();
 *
 * console.log('Coverage:');
 * console.log(`  Statements: ${coverage.statements.percent}%`);
 * console.log(`  Branches: ${coverage.branches.percent}%`);
 * console.log(`  Functions: ${coverage.functions.percent}%`);
 * console.log(`  Lines: ${coverage.lines.percent}%`);
 *
 * // Find files with low coverage
 * const lowCoverage = coverage.files.filter(f => f.lines < 70);
 * console.log(`Files below 70%: ${lowCoverage.length}`);
 * ```
 */
export async function getCoverageReport(): Promise<CoverageReport> {
  return callMCPTool<CoverageReport>("testing__get_coverage_report", {});
}

/**
 * Get coverage summary (lightweight version).
 *
 * @returns Summary with target comparison
 *
 * @example
 * ```typescript
 * const summary = await getCoverageSummary();
 *
 * console.log('Coverage Summary:');
 * console.log(`  Overall: ${summary.overall.lines}% lines`);
 * console.log(`  Meets target (70%): ${summary.meetsTarget ? '✓' : '✗'}`);
 *
 * if (summary.needsImprovement.length > 0) {
 *   console.log('Files needing improvement:');
 *   summary.needsImprovement.forEach(file => console.log(`  - ${file}`));
 * }
 * ```
 */
export async function getCoverageSummary(): Promise<CoverageSummary> {
  return callMCPTool<CoverageSummary>("testing__get_coverage_summary", {});
}

/**
 * Get coverage for specific file.
 *
 * @param filePath - File path to check
 * @returns File coverage data
 *
 * @example
 * ```typescript
 * const coverage = await getFileCoverage('lib/auth.ts');
 * console.log(`${coverage.filePath}: ${coverage.lines}% lines covered`);
 * ```
 */
export async function getFileCoverage(filePath: string): Promise<{
  filePath: string;
  statements: number;
  branches: number;
  functions: number;
  lines: number;
}> {
  return callMCPTool("testing__get_file_coverage", { filePath });
}

/**
 * Run tests with coverage and return only files below threshold.
 *
 * @param threshold - Coverage threshold (default: 70)
 * @returns Files below threshold
 *
 * @example
 * ```typescript
 * const lowFiles = await getFilesBelowThreshold(70);
 *
 * console.log(`${lowFiles.length} files below 70% coverage:`);
 * lowFiles.forEach(f => {
 *   console.log(`  ${f.path}: ${f.lines}% lines`);
 * });
 * ```
 */
export async function getFilesBelowThreshold(threshold = 70): Promise<
  Array<{
    path: string;
    statements: number;
    branches: number;
    functions: number;
    lines: number;
  }>
> {
  return callMCPTool("testing__get_files_below_threshold", { threshold });
}

export default {
  getCoverageReport,
  getCoverageSummary,
  getFileCoverage,
  getFilesBelowThreshold,
};
