/**
 * SKILL: Run Full Test Suite
 *
 * Execute complete testing workflow: unit tests, E2E tests, coverage analysis.
 * Provides comprehensive test report with intelligent filtering.
 *
 * Token Savings: 25,000+ (multiple test runs + parsing → single comprehensive report)
 *
 * Use this skill when:
 * - Pre-deployment validation
 * - After major code changes
 * - Weekly quality checks
 * - CI/CD pipeline simulation
 */

import * as testing from "../servers/testing";

export interface FullTestSuiteResult {
  summary: {
    overallSuccess: boolean;
    totalTests: number;
    totalPassed: number;
    totalFailed: number;
    duration: number;
  };
  unit: {
    success: boolean;
    testsRun: number;
    testsPassed: number;
    testsFailed: number;
    failures?: string[];
  };
  e2e: {
    success: boolean;
    testsRun: number;
    testsPassed: number;
    testsFailed: number;
    failures?: string[];
  };
  coverage: {
    statements: number;
    branches: number;
    functions: number;
    lines: number;
    meetsTarget: boolean; // 70% target
    filesBelowThreshold?: string[];
  };
  recommendations: string[];
}

/**
 * Run complete test suite and generate comprehensive report.
 *
 * @returns Complete test results with recommendations
 *
 * @example
 * ```typescript
 * console.log('🧪 Running full test suite...\n');
 *
 * const report = await runFullTestSuite();
 *
 * console.log('📊 Test Suite Results:');
 * console.log(`  Overall: ${report.summary.overallSuccess ? '✓ PASS' : '✗ FAIL'}`);
 * console.log(`  Tests: ${report.summary.totalPassed}/${report.summary.totalTests} passed`);
 * console.log(`  Duration: ${report.summary.duration}ms\n`);
 *
 * console.log('Unit Tests:');
 * console.log(`  ${report.unit.testsPassed}/${report.unit.testsRun} passed`);
 *
 * console.log('E2E Tests:');
 * console.log(`  ${report.e2e.testsPassed}/${report.e2e.testsRun} passed`);
 *
 * console.log('Coverage:');
 * console.log(`  Lines: ${report.coverage.lines}%`);
 * console.log(`  Branches: ${report.coverage.branches}%`);
 * console.log(`  Target (70%): ${report.coverage.meetsTarget ? '✓' : '✗'}`);
 *
 * if (report.recommendations.length > 0) {
 *   console.log('\n💡 Recommendations:');
 *   report.recommendations.forEach(r => console.log(`  - ${r}`));
 * }
 * ```
 */
export async function runFullTestSuite(): Promise<FullTestSuiteResult> {
  const startTime = Date.now();

  // Run all tests in parallel
  const [unitResults, e2eResults, coverageResults] = await Promise.all([
    testing.runUnitTestsWithCoverage().catch((err) => ({
      success: false,
      output: "",
      testsRun: 0,
      testsPassed: 0,
      testsFailed: 0,
      coverage: undefined,
    })),
    testing.runE2ETests().catch((err) => ({
      success: false,
      output: "",
      testsRun: 0,
      testsPassed: 0,
      testsFailed: 0,
    })),
    testing.getCoverageSummary().catch((err) => ({
      overall: { statements: 0, branches: 0, functions: 0, lines: 0 },
      meetsTarget: false,
      needsImprovement: [],
    })),
  ]);

  const duration = Date.now() - startTime;

  // Build comprehensive report
  const totalTests = (unitResults.testsRun || 0) + (e2eResults.testsRun || 0);
  const totalPassed =
    (unitResults.testsPassed || 0) + (e2eResults.testsPassed || 0);
  const totalFailed =
    (unitResults.testsFailed || 0) + (e2eResults.testsFailed || 0);

  const overallSuccess =
    unitResults.success && e2eResults.success && coverageResults.meetsTarget;

  // Generate recommendations
  const recommendations: string[] = [];

  if (!unitResults.success) {
    recommendations.push(`Fix ${unitResults.testsFailed} failing unit tests`);
  }

  if (!e2eResults.success) {
    recommendations.push(`Fix ${e2eResults.testsFailed} failing E2E tests`);
  }

  if (!coverageResults.meetsTarget) {
    recommendations.push(
      `Increase coverage to 70% (currently ${coverageResults.overall.lines}% lines)`,
    );
  }

  if (coverageResults.needsImprovement.length > 0) {
    recommendations.push(
      `Add tests for ${coverageResults.needsImprovement.length} files below threshold`,
    );
  }

  return {
    summary: {
      overallSuccess,
      totalTests,
      totalPassed,
      totalFailed,
      duration,
    },
    unit: {
      success: unitResults.success,
      testsRun: unitResults.testsRun || 0,
      testsPassed: unitResults.testsPassed || 0,
      testsFailed: unitResults.testsFailed || 0,
    },
    e2e: {
      success: e2eResults.success,
      testsRun: e2eResults.testsRun || 0,
      testsPassed: e2eResults.testsPassed || 0,
      testsFailed: e2eResults.testsFailed || 0,
    },
    coverage: {
      statements: coverageResults.overall.statements,
      branches: coverageResults.overall.branches,
      functions: coverageResults.overall.functions,
      lines: coverageResults.overall.lines,
      meetsTarget: coverageResults.meetsTarget,
      filesBelowThreshold: coverageResults.needsImprovement,
    },
    recommendations,
  };
}

/**
 * Quick test check (unit tests only, no coverage).
 *
 * @returns Quick test result
 *
 * @example
 * ```typescript
 * const quick = await quickTestCheck();
 * console.log(quick.success ? '✓ Tests passing' : '✗ Tests failing');
 * ```
 */
export async function quickTestCheck(): Promise<{
  success: boolean;
  testsRun: number;
  testsPassed: number;
}> {
  const results = await testing.runUnitTests();
  return {
    success: results.success,
    testsRun: results.testsRun || 0,
    testsPassed: results.testsPassed || 0,
  };
}

export default {
  runFullTestSuite,
  quickTestCheck,
};
