<?php
/**
 * Find untranslated JavaScript strings in ai-eshop-chat.php
 *
 * @package AI_eShop_Optimizer
 */

// Direct access protection - allows CLI execution for development
if ( ! defined( 'ABSPATH' ) ) {
    if ( php_sapi_name() !== 'cli' ) {
        exit;
    }
}

$file = __DIR__ . '/assets/pages/ai-eshop-chat.php';
$content = file_get_contents($file);

// Extract only the JavaScript section (between <script> tags)
preg_match('/<script>(.*?)<\/script>/s', $content, $matches);
if (!isset($matches[1])) {
    echo "No script section found\n";
    exit;
}

$jsContent = $matches[1];
$lines = explode("\n", $jsContent);

// Find strings in single or double quotes that look like English text
$untranslated = [];
$lineNumber = 0;

// Find the line number where <script> starts in the original file
$originalLines = explode("\n", $content);
$scriptStartLine = 0;
foreach ($originalLines as $i => $line) {
    if (strpos($line, '<script>') !== false) {
        $scriptStartLine = $i + 1;
        break;
    }
}

foreach ($lines as $index => $line) {
    $actualLineNumber = $scriptStartLine + $index + 1;

    // Skip lines that already use translation functions
    if (strpos($line, 'esc_html_e') !== false ||
        strpos($line, 'esc_html__') !== false ||
        strpos($line, '__(') !== false) {
        continue;
    }

    // Skip console.log and comments
    $trimmed = trim($line);
    if (strpos($line, 'console.') !== false || (strlen($trimmed) >= 2 && $trimmed[0] === '/' && $trimmed[1] === '/')) {
        continue;
    }

    // Find strings with actual English words (not just symbols or single chars)
    // Match single-quoted strings
    if (preg_match_all("/'([^']{3,})'/", $line, $matches)) {
        foreach ($matches[1] as $str) {
            // Check if it contains English letters and looks like user-facing text
            if (preg_match('/[a-zA-Z]{2,}/', $str) &&
                !preg_match('/^(info|success|error|warning|progress)$/', $str) && // Skip type keywords
                strpos($str, 'aieo') === false && // Skip CSS classes
                strpos($str, 'action') === false && // Skip action names
                strpos($str, '_') === false) { // Skip snake_case identifiers
                $untranslated[] = [
                    'line' => $actualLineNumber,
                    'string' => $str,
                    'context' => trim($line)
                ];
            }
        }
    }

    // Match double-quoted strings
    if (preg_match_all('/"([^"]{3,})"/', $line, $matches)) {
        foreach ($matches[1] as $str) {
            // Check if it contains English letters and looks like user-facing text
            if (preg_match('/[a-zA-Z]{2,}/', $str) &&
                !preg_match('/^(info|success|error|warning|progress)$/', $str) &&
                strpos($str, 'aieo') === false &&
                strpos($str, 'action') === false &&
                strpos($str, '_') === false) {
                $untranslated[] = [
                    'line' => $actualLineNumber,
                    'string' => $str,
                    'context' => trim($line)
                ];
            }
        }
    }
}

echo "Found " . count($untranslated) . " potentially untranslated JavaScript strings:\n\n";

foreach ($untranslated as $i => $item) {
    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CLI tool output
    echo ($i + 1) . ". Line " . $item['line'] . ": \"" . $item['string'] . "\"\n";
    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CLI tool output
    echo "   Context: " . substr($item['context'], 0, 100) . "\n\n";
}
