<?php
$logPath = "C:/Users/nagek/.gemini/antigravity-ide/brain/bae110df-8171-4e55-8939-1194d0bd26dc/.system_generated/logs/transcript_full.jsonl";
$lines = file($logPath);

$filesToRecover = [
    'accounts.blade.php',
    'projects.blade.php',
    'ledgers.blade.php',
    'parties.blade.php',
    'branches.blade.php',
    'centers.blade.php',
    'entities.blade.php'
];

$fileStates = [];

foreach ($lines as $line) {
    $data = json_decode($line, true);
    if (!$data) continue;
    
    // Tool Calls
    if ($data['type'] === 'PLANNER_RESPONSE' && !empty($data['tool_calls'])) {
        foreach ($data['tool_calls'] as $call) {
            if ($call['name'] === 'default_api:multi_replace_file_content') {
                $targetFile = $call['args']['TargetFile'];
                $basename = basename(trim($targetFile, '"'));
                if (in_array($basename, $filesToRecover) && isset($fileStates[$basename])) {
                    $chunks = $call['args']['ReplacementChunks'];
                    foreach ($chunks as $chunk) {
                        $target = $chunk['TargetContent'];
                        $replacement = $chunk['ReplacementContent'];
                        // apply replacement
                        $fileStates[$basename] = str_replace($target, $replacement, $fileStates[$basename]);
                    }
                }
            }
        }
    }
    
    // Tool Responses (for view_file)
    if ($data['type'] === 'TOOL_RESPONSE' && !empty($data['content'])) {
        $content = $data['content'];
        // extract file path
        if (preg_match('/File Path: `file:\/\/\/(.*?)\`/', $content, $matches)) {
            $path = $matches[1];
            $basename = basename($path);
            if (in_array($basename, $filesToRecover)) {
                // extract file content (remove "1: " prefix)
                $lines = explode("\n", $content);
                $fileContent = [];
                $capture = false;
                foreach ($lines as $l) {
                    if (strpos($l, 'The following code has been modified to include a line number') !== false) {
                        $capture = true;
                        continue;
                    }
                    if (strpos($l, 'The above content shows the entire, complete file contents') !== false) {
                        $capture = false;
                        continue;
                    }
                    if ($capture) {
                        // remove "123: "
                        $fileContent[] = preg_replace('/^\d+:\s?/', '', $l);
                    }
                }
                if (!empty($fileContent)) {
                    $fileStates[$basename] = implode("\n", $fileContent);
                }
            }
        }
    }
}

foreach ($fileStates as $name => $content) {
    // Finally apply the overflow-hidden fix that we wanted to do in the first place!
    $content = str_replace('<div class="bg-white rounded-2xl shadow-sm border border-slate-100 overflow-hidden">', '<div class="bg-white rounded-2xl shadow-sm border border-slate-100 overflow-visible">', $content);
    $content = str_replace('<div class="p-4 border-b border-slate-100 bg-slate-50/50 flex', '<div class="p-4 border-b border-slate-100 bg-slate-50/50 relative z-20 flex', $content);
    $content = str_replace('<div class="p-4 border-b border-slate-100 bg-slate-50/50 rounded-t-2xl">', '<div class="p-4 border-b border-slate-100 bg-slate-50/50 relative z-20 rounded-t-2xl">', $content);
    
    file_put_contents("d:/laragon/www/Ledger/resources/views/livewire/$name", $content);
    echo "Recovered $name\n";
}
