/**
 * Shared validation result formatting utilities
 *
 * Provides common formatting patterns for validation results.
 * Domain-specific formatters (CSS, template, etc.) can extend these.
 */

import chalk from 'chalk';

/**
 * Format a generic validation result summary
 *
 * @param {Object} summary - Validation summary object
 * @param {string} title - Title for the results section
 * @returns {string[]} Array of formatted lines
 */
export function formatValidationSummary(summary, title = 'Validation Results') {
  const lines = [];

  lines.push('');
  lines.push(title);
  lines.push('='.repeat(50));
  lines.push('');

  // Format summary key-value pairs
  for (const [key, value] of Object.entries(summary)) {
    const label = key
      .replace(/([A-Z])/g, ' $1')
      .replace(/^./, str => str.toUpperCase())
      .trim();
    lines.push(`${label}: ${value}`);
  }

  lines.push('');

  return lines;
}

/**
 * Format validation issues with consistent styling
 *
 * @param {Array} issues - Array of validation issues
 * @param {boolean} verbose - Include detailed output
 * @returns {string[]} Array of formatted issue lines
 */
export function formatValidationIssues(issues, verbose = false) {
  const lines = [];

  if (issues.length === 0) {
    return lines;
  }

  // Group by type
  const errors = issues.filter(i => i.type === 'error');
  const warnings = issues.filter(i => i.type === 'warning');

  if (errors.length > 0) {
    lines.push(chalk.red.bold(`Errors (${errors.length}):`));
    lines.push('-'.repeat(50));
    for (const issue of errors) {
      lines.push(formatIssue(issue, verbose));
    }
    lines.push('');
  }

  if (warnings.length > 0) {
    lines.push(chalk.yellow.bold(`Warnings (${warnings.length}):`));
    lines.push('-'.repeat(50));
    for (const issue of warnings) {
      lines.push(formatIssue(issue, verbose));
    }
    lines.push('');
  }

  return lines;
}

/**
 * Format a single validation issue
 *
 * @param {Object} issue - Validation issue
 * @param {boolean} verbose - Include additional details
 * @returns {string} Formatted issue string
 */
function formatIssue(issue, verbose = false) {
  const color = issue.type === 'error' ? chalk.red : chalk.yellow;
  const icon = issue.type === 'error' ? '✗' : '⚠';

  let formatted = `  ${icon} ${issue.message}`;

  if (issue.file) {
    formatted += chalk.dim(` (${issue.file}`);
    if (issue.line) {
      formatted += `:${issue.line}`;
    }
    formatted += chalk.dim(')');
  }

  if (verbose && issue.suggestion) {
    formatted += `\n    ${chalk.cyan('→')} ${issue.suggestion}`;
  }

  return formatted;
}

/**
 * Format final validation status
 *
 * @param {boolean} valid - Whether validation passed
 * @param {number} errorCount - Number of errors
 * @param {number} warningCount - Number of warnings
 * @returns {string} Status message with appropriate styling
 */
export function formatValidationStatus(valid, errorCount = 0, warningCount = 0) {
  if (valid && errorCount === 0 && warningCount === 0) {
    return chalk.green('✓ All validations passed');
  }

  if (errorCount > 0) {
    return chalk.red(`✗ Validation failed with ${errorCount} error(s) and ${warningCount} warning(s)`);
  }

  return chalk.yellow(`⚠ Validation passed with ${warningCount} warning(s)`);
}
