<?php

function build_google($input_path, $output_path) {
    $version = '';
    $by_path = array(); 
    $rows    = array();
    foreach (file($input_path) as $line) {
        $line = trim($line);
        if ($line === '') continue;
        if ($line[0] === '#') {
            if (preg_match('/Version:\s*(\S+)/', $line, $m)) $version = $m[1];
            continue;
        }
        if (!preg_match('/^(\d+)\s*-\s*(.+)$/u', $line, $m)) continue;
        $id   = (int) $m[1];
        $path = trim($m[2]);
        $rows[] = array('id' => $id, 'path' => $path);
        $by_path[$path] = $id;
    }
    
    $nodes = array();
    $by_id = array();
    $roots = array();
    foreach ($rows as $r) {
        $parts = explode(' > ', $r['path']);
        $name  = end($parts);
        $depth = count($parts);
        $parent_path = $depth > 1 ? implode(' > ', array_slice($parts, 0, -1)) : null;
        $parent_id   = ($parent_path !== null && isset($by_path[$parent_path]))
            ? $by_path[$parent_path] : null;
        $idx = count($nodes);
        $nodes[] = array(
            'i' => $r['id'],
            'n' => $name,
            'p' => $parent_id,
            'd' => $depth,
            'h' => $r['path'],
        );
        $by_id[(string) $r['id']] = $idx;
        if ($parent_id === null) $roots[] = $idx;
    }
    $payload = array(
        'source'  => 'google',
        'version' => $version,
        'count'   => count($nodes),
        'nodes'   => $nodes,
        'by_id'   => $by_id,
        'roots'   => $roots,
    );
    file_put_contents($output_path, json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
    echo "  google → " . basename($output_path) . " : " . count($nodes) . " nodes, "
       . round(filesize($output_path) / 1024) . " KB\n";
}

function build_facebook($input_path, $output_path) {
    $by_path = array();
    $rows    = array();
    $h = fopen($input_path, 'r');
    if (!$h) { echo "  ERROR opening $input_path\n"; return; }
    
    $first = fgets($h);
    while (($line = fgets($h)) !== false) {
        $line = trim($line);
        if ($line === '') continue;
        
        if (!preg_match('/^(\d+)\s*,\s*(.+)$/u', $line, $m)) continue;
        $id   = (int) $m[1];
        $path_lower = trim($m[2]);
        
        $parts = explode(' > ', $path_lower);
        $tc = array();
        foreach ($parts as $p) {
            $tc[] = mb_convert_case($p, MB_CASE_TITLE, 'UTF-8');
        }
        $path = implode(' > ', $tc);
        $rows[] = array('id' => $id, 'path' => $path);
        $by_path[$path] = $id;
    }
    fclose($h);
    $nodes = array();
    $by_id = array();
    $roots = array();
    foreach ($rows as $r) {
        $parts = explode(' > ', $r['path']);
        $name  = end($parts);
        $depth = count($parts);
        $parent_path = $depth > 1 ? implode(' > ', array_slice($parts, 0, -1)) : null;
        $parent_id   = ($parent_path !== null && isset($by_path[$parent_path]))
            ? $by_path[$parent_path] : null;
        $idx = count($nodes);
        $nodes[] = array(
            'i' => $r['id'],
            'n' => $name,
            'p' => $parent_id,
            'd' => $depth,
            'h' => $r['path'],
        );
        $by_id[(string) $r['id']] = $idx;
        if ($parent_id === null) $roots[] = $idx;
    }
    $payload = array(
        'source'  => 'facebook',
        'version' => date('Y-m-d') . ' (snapshot)',
        'count'   => count($nodes),
        'nodes'   => $nodes,
        'by_id'   => $by_id,
        'roots'   => $roots,
    );
    file_put_contents($output_path, json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
    echo "  facebook → " . basename($output_path) . " : " . count($nodes) . " nodes, "
       . round(filesize($output_path) / 1024) . " KB\n";
}

$dir = __DIR__;
echo "Building Google taxonomies + Facebook taxonomy...\n";
build_google("$dir/google-taxonomy-with-ids.en-US.txt", "$dir/google-taxonomy.en-US.json");
build_google("$dir/google-taxonomy-with-ids.el-GR.txt", "$dir/google-taxonomy.el-GR.json");
build_facebook("$dir/facebook-taxonomy.en-US.txt",       "$dir/facebook-taxonomy.en-US.json");
echo "Done.\n";
