<?php
// Read the full home.php
$lines = file('c:/Users/tiepb/Downloads/Company/agent_test/data/home/home.php');
$totalLines = count($lines);

// Find "article_home" line
$articleHomeStart = null;
foreach ($lines as $i => $line) {
    if (strpos($line, '"article_home"') !== false) {
        $articleHomeStart = $i;
        break;
    }
}

echo "article_home starts at line: " . ($articleHomeStart + 1) . "\n";

// Extract everything from article_home to end (before ");")
$rawSection = implode('', array_slice($lines, $articleHomeStart + 1, $totalLines - $articleHomeStart - 2));
echo "Raw section length: " . strlen($rawSection) . "\n";

// Find all category keys with their positions
preg_match_all('/^ *(\d+) *=>\s*\n/m', $rawSection, $matches, PREG_OFFSET_CAPTURE);
echo "Category keys found: ";
foreach ($matches[1] as $m) {
    echo $m[0] . " ";
}
echo "\n";

// Build sections
$catKeys = [];
$catPositions = [];
foreach ($matches[1] as $i => $m) {
    $catKeys[] = (int)$m[0];
    // Find the [ after this position
    $afterHeader = $matches[0][$i][1] + strlen($matches[0][$i][0]);
    $catPositions[] = $afterHeader;
}

$sections = [];
for ($i = 0; $i < count($catKeys); $i++) {
    $start = $catPositions[$i];
    $end = isset($catPositions[$i+1]) ? $catPositions[$i+1] - strlen($matches[0][$i+1][0]) : strlen($rawSection);
    $chunk = trim(substr($rawSection, $start, $end - $start));

    $data = json_decode($chunk, true);
    if (json_last_error() === JSON_ERROR_NONE) {
        $sections[$catKeys[$i]] = $data;
        echo "Category " . $catKeys[$i] . ": " . count($data) . " articles\n";
    } else {
        echo "Category " . $catKeys[$i] . ": JSON ERROR - " . json_last_error_msg() . "\n";
        // Show first 200 chars of chunk
        echo "Chunk: " . substr($chunk, 0, 200) . "\n";
    }
}

if (empty($sections)) {
    echo "No sections parsed, exiting\n";
    exit(1);
}

// Helper function to export value as PHP array
function phpExport($val, $indent = 0) {
    $pad = str_repeat('  ', $indent);
    $padInner = str_repeat('  ', $indent + 1);

    if (is_null($val)) return 'NULL';
    if (is_bool($val)) return $val ? 'true' : 'false';
    if (is_int($val)) return (string)$val;
    if (is_float($val)) return (string)$val;
    if (is_string($val)) {
        $escaped = str_replace("\\", "\\\\", $val);
        $escaped = str_replace("'", "\\'", $escaped);
        return "'" . $escaped . "'";
    }
    if (is_array($val)) {
        if (empty($val)) return "array (\n$pad)";
        $items = [];
        foreach ($val as $k => $v) {
            $keyStr = is_int($k) ? "$k => " : "'" . addcslashes($k, "'\\") . "' => ";
            $items[] = $padInner . $keyStr . phpExport($v, $indent + 1);
        }
        return "array (\n" . implode(",\n", $items) . ",\n$pad)";
    }
    return var_export($val, true);
}

// Keep lines 1 to articleHomeStart (0-indexed = lines 1..articleHomeStart)
$header = implode('', array_slice($lines, 0, $articleHomeStart));

// Build article_home PHP array
$output = $header;
$output .= "  'article_home' => \n";
$output .= "  array (\n";

foreach ($sections as $catId => $articles) {
    $output .= "    $catId => \n";
    $output .= "    array (\n";
    foreach ($articles as $idx => $article) {
        $output .= "      $idx => \n";
        $output .= "      " . phpExport($article, 3) . ",\n";
    }
    $output .= "    ),\n";
}

$output .= "  ),\n";
$output .= ");\n";

file_put_contents('c:/Users/tiepb/Downloads/Company/agent_test/data/home/home.php', $output);
echo "Done! Written " . strlen($output) . " bytes\n";

$result = shell_exec('php -l "c:/Users/tiepb/Downloads/Company/agent_test/data/home/home.php" 2>&1');
echo "PHP syntax: $result\n";
