<?php
$content = file_get_contents('scratch/extracted_dumps.txt');
$blocks = explode('Array', $content);

echo "Found " . count($blocks) . " database record printouts in dumps.\n";

$stripesData = [];

foreach ($blocks as $block) {
    if (strpos($block, '[image_climate_stripe_url]') !== false || strpos($block, '[image_stripe_ssp126_url]') !== false) {
        // Parse this block to extract variable, period, and stripe URLs
        preg_match('/\[variable\]\s*=>\s*([^\n]+)/', $block, $varMatch);
        preg_match('/\[variable_id\]\s*=>\s*([0-9]+)/', $block, $varIdMatch);
        preg_match('/\[period\]\s*=>\s*([^\n]+)/', $block, $perMatch);
        preg_match('/\[period_id\]\s*=>\s*([0-9]+)/', $block, $perIdMatch);
        
        $variable = isset($varMatch[1]) ? trim($varMatch[1]) : '';
        if (!$variable && isset($varIdMatch[1])) {
            $id = intval($varIdMatch[1]);
            // map default ids
            $varMap = [
                1 => 'Precipitation', 2 => 'Suhu Udara Rata-rata', 3 => 'Suhu Udara Maksimum',
                4 => 'Suhu Udara Minimum', 10 => 'Salinitas Permukaan Laut', 11 => 'pH Permukaan Laut'
            ];
            $variable = $varMap[$id] ?? "Var-$id";
        }
        
        $period = isset($perMatch[1]) ? trim($perMatch[1]) : '';
        if (!$period && isset($perIdMatch[1])) {
            $id = intval($perIdMatch[1]);
            $perMap = [1 => '2021-2050', 2 => '2031-2060', 3 => '2041-2070', 4 => '2051-2080', 5 => '2061-2090', 6 => '2071-2100'];
            $period = $perMap[$id] ?? "Per-$id";
        }

        preg_match('/\[image_climate_stripe_url\]\s*=>\s*([^\n]*)/', $block, $stripeMatch);
        preg_match('/\[image_stripe_ssp126_url\]\s*=>\s*([^\n]*)/', $block, $ssp126Match);
        preg_match('/\[image_stripe_ssp245_url\]\s*=>\s*([^\n]*)/', $block, $ssp245Match);
        preg_match('/\[image_stripe_ssp370_url\]\s*=>\s*([^\n]*)/', $block, $ssp370Match);
        preg_match('/\[image_stripe_ssp585_url\]\s*=>\s*([^\n]*)/', $block, $ssp585Match);
        
        $stripe = isset($stripeMatch[1]) ? trim($stripeMatch[1]) : '';
        $ssp126 = isset($ssp126Match[1]) ? trim($ssp126Match[1]) : '';
        $ssp245 = isset($ssp245Match[1]) ? trim($ssp245Match[1]) : '';
        $ssp370 = isset($ssp370Match[1]) ? trim($ssp370Match[1]) : '';
        $ssp585 = isset($ssp585Match[1]) ? trim($ssp585Match[1]) : '';
        
        if ($variable && $period) {
            $key = $variable . " | " . $period;
            if (!isset($stripesData[$key])) {
                $stripesData[$key] = [
                    'variable' => $variable,
                    'period' => $period,
                    'stripe' => $stripe,
                    'ssp126' => $ssp126,
                    'ssp245' => $ssp245,
                    'ssp370' => $ssp370,
                    'ssp585' => $ssp585
                ];
            } else {
                // Update if we find non-empty fields
                if ($stripe) $stripesData[$key]['stripe'] = $stripe;
                if ($ssp126) $stripesData[$key]['ssp126'] = $ssp126;
                if ($ssp245) $stripesData[$key]['ssp245'] = $ssp245;
                if ($ssp370) $stripesData[$key]['ssp370'] = $ssp370;
                if ($ssp585) $stripesData[$key]['ssp585'] = $ssp585;
            }
        }
    }
}

echo "\nParsed Stripe Mappings from Logs:\n";
foreach ($stripesData as $key => $d) {
    if (!empty(array_filter([$d['stripe'], $d['ssp126'], $d['ssp245'], $d['ssp370'], $d['ssp585']]))) {
        echo "Key: $key\n";
        if ($d['stripe']) echo "  Global Stripe: " . $d['stripe'] . "\n";
        if ($d['ssp126']) echo "  SSP1-2.6: " . $d['ssp126'] . "\n";
        if ($d['ssp245']) echo "  SSP2-4.5: " . $d['ssp245'] . "\n";
        if ($d['ssp370']) echo "  SSP3-7.0: " . $d['ssp370'] . "\n";
        if ($d['ssp585']) echo "  SSP5-8.5: " . $d['ssp585'] . "\n";
        echo "---------------------------------\n";
    }
}
