const fs = require('fs');
const readline = require('readline');
const path = require('path');

const logPath = 'C:/Users/BS/.gemini/antigravity/brain/393257b2-e812-421c-92f7-099f2362049e/.system_generated/logs/transcript.jsonl';

async function extract() {
    const fileStream = fs.createReadStream(logPath);
    const rl = readline.createInterface({
        input: fileStream,
        crlfDelay: Infinity
    });

    let appFinal = '';
    let indexHtml = '';
    const cutoffTime = '2026-06-03T06:15:00Z'; // 9:15 AM local time

    for await (const line of rl) {
        try {
            const entry = JSON.parse(line);
            
            // Stop processing if we passed 9:15 AM
            if (entry.created_at && entry.created_at > cutoffTime) {
                break;
            }

            if (entry.tool_calls) {
                for (const tool of entry.tool_calls) {
                    if (tool.function && tool.function.arguments) {
                        try {
                            const args = JSON.parse(tool.function.arguments);
                            
                            // Handling write_to_file
                            if (tool.function.name === 'write_to_file' || tool.function.name === 'default_api:write_to_file') {
                                if (args.TargetFile && args.TargetFile.endsWith('app_final.js')) {
                                    appFinal = args.CodeContent;
                                }
                                if (args.TargetFile && args.TargetFile.endsWith('index.html')) {
                                    indexHtml = args.CodeContent;
                                }
                            }
                            
                            // Handling replace_file_content
                            if (tool.function.name === 'replace_file_content' || tool.function.name === 'default_api:replace_file_content') {
                                if (args.TargetFile && args.TargetFile.endsWith('app_final.js') && appFinal) {
                                    if (appFinal.includes(args.TargetContent)) {
                                        appFinal = appFinal.replace(args.TargetContent, args.ReplacementContent);
                                    }
                                }
                                if (args.TargetFile && args.TargetFile.endsWith('index.html') && indexHtml) {
                                    if (indexHtml.includes(args.TargetContent)) {
                                        indexHtml = indexHtml.replace(args.TargetContent, args.ReplacementContent);
                                    }
                                }
                            }
                        } catch(e) {}
                    }
                }
            }
        } catch (e) {}
    }

    console.log("Restored app_final.js length:", appFinal.length);
    console.log("Restored index.html length:", indexHtml.length);

    if (appFinal.length > 0) {
        fs.writeFileSync('d:/برنامج مخازن/app_final_9am.js', appFinal, 'utf8');
    }
    if (indexHtml.length > 0) {
        fs.writeFileSync('d:/برنامج مخازن/index_9am.html', indexHtml, 'utf8');
    }
}
extract();
