<?php
/**
 * 247ChatMate i18n audit.
 * Run from plugin root: php tools/i18n-audit.php
 * Hard rule: widget-ui and sales-demo-chips language files must match en-gb keys.
 */
$root = dirname(__DIR__);
$i18n = $root . '/public/i18n';
function read_json_file($path) {
    if (!is_file($path)) return null;
    $data = json_decode(file_get_contents($path), true);
    return is_array($data) ? $data : null;
}
function sorted_keys($arr) { $keys = array_keys($arr ?: []); sort($keys); return $keys; }
$langs = read_json_file($i18n . '/languages.json') ?: [];
$uiBase = read_json_file($i18n . '/widget-ui/en-gb.json') ?: [];
$chipBaseDoc = read_json_file($i18n . '/sales-demo-chips/en-gb.json') ?: [];
$chipBase = isset($chipBaseDoc['chips']) && is_array($chipBaseDoc['chips']) ? $chipBaseDoc['chips'] : [];
$report = [
    'schema' => '247chatmate.i18n.audit.v1',
    'baseline' => 'en-gb',
    'rules' => [
        'No live widget text outside JSON source of truth.',
        'When language changes, widget-ui and sales-demo-chips must both load before render.',
        'Missing keys must be fixed in JSON, not patched in JavaScript.',
        'Language selector is separate from phone country code selector.',
        'Hard reset restores the website/default language and returns to the start of the chat.'
    ],
    'summary' => ['languages' => count($langs), 'failed' => 0, 'passed' => 0],
    'languages' => []
];
$uiKeys = sorted_keys($uiBase);
$chipKeys = sorted_keys($chipBase);
foreach ($langs as $code => $meta) {
    $uiPath = $i18n . '/' . ($meta['ui'] ?? ('widget-ui/' . $code . '.json'));
    $chipPath = $i18n . '/' . ($meta['chips'] ?? ('sales-demo-chips/' . $code . '.json'));
    $ui = read_json_file($uiPath);
    $chipDoc = read_json_file($chipPath);
    $chips = (isset($chipDoc['chips']) && is_array($chipDoc['chips'])) ? $chipDoc['chips'] : [];
    $missingUi = array_values(array_diff($uiKeys, sorted_keys($ui ?: [])));
    $missingChips = array_values(array_diff($chipKeys, sorted_keys($chips)));
    $sameUi = [];
    $sameChips = [];
    if ($code !== 'en-gb') {
        foreach ($uiBase as $k => $v) {
            if (isset($ui[$k]) && trim((string) $ui[$k]) === trim((string) $v)) $sameUi[] = $k;
        }
        foreach ($chipBase as $k => $v) {
            if (isset($chips[$k]) && trim((string) $chips[$k]) === trim((string) $v)) $sameChips[] = $k;
        }
    }
    $entry = [
        'code' => $code,
        'nativeName' => $meta['nativeName'] ?? $code,
        'rtl' => !empty($meta['rtl']),
        'ui_file_exists' => is_file($uiPath),
        'chips_file_exists' => is_file($chipPath),
        'missing_widget_ui_keys_count' => count($missingUi),
        'missing_chip_keys_count' => count($missingChips),
        'english_like_widget_ui_values_count' => count($sameUi),
        'english_like_chip_values_count' => count($sameChips),
        'missing_widget_ui_keys' => $missingUi,
        'missing_chip_keys' => $missingChips,
        'english_like_widget_ui_values' => $sameUi,
        'english_like_chip_values' => $sameChips
    ];
    $ok = $entry['ui_file_exists'] && $entry['chips_file_exists'] && !$missingUi && !$missingChips;
    $entry['status'] = $ok ? 'pass' : 'fail';
    $report['summary'][$ok ? 'passed' : 'failed']++;
    $report['languages'][$code] = $entry;
}
$out = $root . '/247chatmate-i18n-audit-report.json';
file_put_contents($out, json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
echo "Wrote {$out}\n";
echo "Passed: {$report['summary']['passed']} Failed: {$report['summary']['failed']}\n";
exit($report['summary']['failed'] ? 1 : 0);
