const { ROOT, DIRS, CONFIG, ensureDir, timestamp, log, writeMarkdown } = require('./utils');
const context = require('./context-manager');
const path = require('path');
const fs = require('fs');
const { execSync, spawn } = require('child_process');

function executeTests(testName, options = {}) {
  const testCfg = CONFIG.test || {};
  const {
    headed = CONFIG.browser && CONFIG.browser.headed || false,
    timeout = testCfg.timeout || 120000,
    retries = testCfg.retries || 0,
    workers = testCfg.workers || 1,
    //screenshots = 'off',
  } = options;

  const runId = `run-${timestamp()}`;
  const runDir = path.join(DIRS.testResults, runId);
  ensureDir(runDir);

  const storyName = testName ? testName.replace(/\.spec\.ts$/, '').split(/[\/\\]/).pop() : null;

  log('EXECUTOR', `Run ID: ${runId}`);
  log('EXECUTOR', `Test: ${testName || 'all tests'}`);
  if (storyName) log('EXECUTOR', `Story: ${storyName}`);

  const args = ['playwright', 'test'];

  if (testName) {
    let testFile = testName.endsWith('.spec.ts') ? testName : `${testName}.spec.ts`;
    if (testFile.includes('/') || testFile.includes('\\')) {
      args.push(testFile);
    } else {
      args.push(path.join('tests', testFile));
    }
  }

  const allureAvailable = (() => {
    try { require.resolve('allure-playwright/package.json'); return true; } catch { return false; }
  })();
  const reporters = ['list', 'json'];
  if (allureAvailable) reporters.push('allure-playwright');
  args.push('--reporter', reporters.join(','));

  if (headed) args.push('--headed');
  if (retries > 0) args.push('--retries', retries.toString());
  if (workers) args.push('--workers', workers.toString());
  if (timeout) args.push('--timeout', timeout.toString());
  // Screenshots configured in playwright.config.ts — not a valid CLI arg

  const outputPath = path.join(runDir, 'execution-output.json');
  const resultPath = path.join(runDir, 'execution-result.json');

  log('EXECUTOR', `Command: npx ${args.join(' ')}`);

  const startTime = Date.now();

  try {
    const stdout = execSync(`npx ${args.join(' ')}`, {
      cwd: ROOT,
      encoding: 'utf-8',
      timeout: 300000,
      maxBuffer: 10 * 1024 * 1024,
      stdio: ['pipe', 'pipe', 'pipe'],
    });

    fs.writeFileSync(outputPath, stdout, 'utf-8');

    const passedTests = extractPassedTests(stdout);

    const result = {
      runId,
      story: storyName,
      test: testName || 'all',
      success: true,
      exitCode: 0,
      duration: Date.now() - startTime,
      timestamp: new Date().toISOString(),
      output: stdout.substring(0, 50000),
      failedTests: [],
      passedTests,
    };

    writeMarkdown(resultPath, JSON.stringify(result, null, 2));
    context.setLastRun(runId);
    if (storyName) {
      context.addRunId(storyName, runId, true, testName);
      context.phaseComplete('execute', storyName);
    }
    log('EXECUTOR', `Tests passed (${result.duration}ms)`);

    generateAllureReport();

    return result;
  } catch (err) {
    const stderr = err.stderr || '';
    const stdout = err.stdout || '';

    const combinedOutput = stdout + '\n' + stderr;
    fs.writeFileSync(outputPath, combinedOutput, 'utf-8');

    const failedTests = extractFailedTests(combinedOutput);
    const passedTests = extractPassedTests(combinedOutput);

    const result = {
      runId,
      story: storyName,
      test: testName || 'all',
      success: false,
      exitCode: err.status || 1,
      duration: Date.now() - startTime,
      timestamp: new Date().toISOString(),
      error: err.message,
      output: combinedOutput.substring(0, 50000),
      failedTests,
      passedTests,
    };

    writeMarkdown(resultPath, JSON.stringify(result, null, 2));
    context.setLastRun(runId);
    if (storyName) {
      context.addRunId(storyName, runId, false, testName);
      context.phaseComplete('execute', storyName);
    }
    log('EXECUTOR', `Tests failed (${result.duration}ms) - ${failedTests.length} failure(s)`);

    generateAllureReport();

    return result;
  }
}

function extractFailedTests(output) {
  const failed = [];
  const lines = output.split('\n');

  let current = null;
  for (const line of lines) {
    const failMatch = line.match(/\s+×\s+\d+\)\s+\[(.+?)\]\s+(.+)/);
    if (failMatch) {
      failed.push({ file: failMatch[1], test: failMatch[2].trim() });
      continue;
    }

    const failSimple = line.match(/\s+×\s+(.+)/);
    if (failSimple && !line.includes('ms')) {
      current = { test: failSimple[1].trim(), error: '' };
      continue;
    }

    if (current && line.trim()) {
      current.error += line.trim() + ' ';
    }

    if (current && line.trim() === '') {
      failed.push(current);
      current = null;
    }
  }

  return failed;
}

function extractPassedTests(output) {
  const passed = [];
  const lines = output.split('\n');

  for (const line of lines) {
    const passMatch = line.match(/\s+√\s+\d+\)\s+\[(.+?)\]\s+(.+)/);
    if (passMatch) {
      passed.push({ file: passMatch[1], test: passMatch[2].trim() });
      continue;
    }

    const passSimple = line.match(/\s+√\s+(.+)/);
    if (passSimple && !line.includes('ms')) {
      passed.push({ test: passSimple[1].trim() });
    }
  }

  return passed;
}

function generateAllureReport() {
  const allureResults = DIRS.allureResults;
  const allureReport = path.join(ROOT, 'allure-report');

  if (!fs.existsSync(allureResults) || fs.readdirSync(allureResults).length === 0) return;

  log('EXECUTOR', 'Auto-generating Allure report...');
  try {
    const { execSync } = require('child_process');
    execSync(`npx allure generate "${allureResults}" --clean -o "${allureReport}"`, {
      cwd: ROOT,
      encoding: 'utf-8',
      timeout: 60000,
      stdio: 'pipe',
    });
    log('EXECUTOR', `Allure report ready: allure-report/index.html`);
  } catch (err) {
    // Allure CLI not available — non-fatal
    log('EXECUTOR', 'Allure CLI not available, skipping HTML report generation');
  }
}

if (require.main === module) {
  const testName = process.argv[2];
  const headed = process.argv.includes('--headed');
  executeTests(testName, { headed });
}

module.exports = { executeTests };
