<?php
$data  = json_decode(file_get_contents('/tmp/db_scan_result.json'), true);
$prod  = $data['prod_tables'];
$local = $data['local_tables'];
sort($prod); sort($local);

echo "=== SOLO EN PRODUCCIÓN (faltan en local) ===" . PHP_EOL;
foreach (array_diff($prod, $local) as $t) {
    echo "  + $t (" . $data['prod_counts'][$t] . " rows)" . PHP_EOL;
}

echo PHP_EOL . "=== SOLO EN LOCAL (faltan en producción) ===" . PHP_EOL;
foreach (array_diff($local, $prod) as $t) {
    echo "  - $t" . PHP_EOL;
}

echo PHP_EOL . "=== TABLAS EN AMBAS — diferencias de columnas ===" . PHP_EOL;
foreach (array_intersect($prod, $local) as $t) {
    $pCols = array_column($data['prod_schema'][$t]['columns'], 'Field');
    $lCols = array_column($data['local_schema'][$t]['columns'], 'Field');
    $onlyProd  = array_diff($pCols, $lCols);
    $onlyLocal = array_diff($lCols, $pCols);
    if ($onlyProd || $onlyLocal) {
        echo "  TABLE $t:" . PHP_EOL;
        foreach ($onlyProd  as $c) echo "    + prod  extra col: $c" . PHP_EOL;
        foreach ($onlyLocal as $c) echo "    - local extra col: $c" . PHP_EOL;
    }
}

echo PHP_EOL . "=== CONTEO DE FILAS PRODUCCIÓN ===" . PHP_EOL;
arsort($data['prod_counts']);
foreach ($data['prod_counts'] as $t => $n) {
    echo "  $t: $n rows" . PHP_EOL;
}
