/**
 * Exception Verifier
 *
 * After restoring placeholders, verify all exception strings
 * appear in the translated output unchanged.
 */

export interface VerificationResult {
  passed: boolean;
  violations: Array<{ exception: string; status: 'missing' | 'modified' }>;
}

/**
 * Verify that all matched exception strings appear in the translated text.
 *
 * If a placeholder was mangled by Gemini (e.g., __EXCPT_0_ missing an underscore),
 * the restoration fails and the original exception text won't be found in the output.
 * This is detected as a violation.
 */
export function verifyExceptions(
  translatedText: string,
  matchedExceptions: string[]
): VerificationResult {
  const violations: Array<{ exception: string; status: 'missing' | 'modified' }> = [];

  for (const exception of matchedExceptions) {
    // Case-insensitive check since the exception could appear in any case
    if (!translatedText.toLowerCase().includes(exception.toLowerCase())) {
      violations.push({ exception, status: 'missing' });
    }
  }

  // Also check for any unreplaced __EXCPT_N__ placeholders (mangled or leftover)
  const leftoverPlaceholders = translatedText.match(/__EXCPT_\d+__/g);
  if (leftoverPlaceholders) {
    for (const placeholder of leftoverPlaceholders) {
      violations.push({ exception: placeholder, status: 'modified' });
    }
  }

  return {
    passed: violations.length === 0,
    violations,
  };
}
