const fs=require('fs');
const {JSDOM}=require('jsdom');
const rawDump = JSON.parse(fs.readFileSync('C:/Users/Variet-Worker/.gemini/antigravity/bridge/dump_html.json', 'utf8'));

// Inject Tailwind script so the mock renders styles properly when opened in a browser
const htmlStr = rawDump.html;
const dom = new JSDOM("<!DOCTYPE html><html><head><script src='https://cdn.tailwindcss.com'></script></head><body>" + htmlStr + "</body></html>");
const document = dom.window.document;

const titleSpan = document.querySelector('span[title^="command("]');
if(titleSpan) {
    let toolContainer = titleSpan.parentElement;
    while(toolContainer && !toolContainer.className.includes("border-gray-500/10") && !toolContainer.className.includes("bg-gray-500/10")) {
        toolContainer = toolContainer.parentElement;
    }
    if(!toolContainer) toolContainer = titleSpan.parentElement;

    const aiChat = document.createElement('div');
    aiChat.className = 'markdown prose';
    aiChat.innerHTML = '<p>안녕하세요! 시스템을 수정하기 위해 요청하신 작업을 시작합니다. <b>디스코드 릴레이 기능 복구</b>를 위해 스크립트를 실행하겠습니다.</p>';

    const parent = toolContainer.parentElement;
    const convoWrapper = document.createElement('div');
    convoWrapper.className = 'bg-agent-convo-background p-4 rounded-lg my-4 bg-gray-800 text-white';
    parent.insertBefore(convoWrapper, toolContainer);
    convoWrapper.appendChild(aiChat);
    convoWrapper.appendChild(toolContainer);

    const btn = document.createElement('button');
    btn.className = 'bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded mt-2';
    btn.innerHTML = '<span class="truncate">Allow</span>';
    toolContainer.appendChild(btn);
}

fs.writeFileSync('C:/Users/Variet-Worker/Desktop/gravity_control/mock_output.html', dom.serialize());
console.log('Saved to mock_output.html');
