// 1. Listen for the Keyboard Shortcut
chrome.commands.onCommand.addListener((command) => {
    if (command === "trigger_aura") {
        chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
            if (tabs[0] && !tabs[0].url.startsWith("chrome://")) {
                chrome.tabs.sendMessage(tabs[0].id, {action: "trigger_crosshair"}).catch(()=>console.log("Tab not ready."));
            }
        });
    }
});

// 2. Listen for the Icon Click
chrome.action.onClicked.addListener((tab) => {
    if (!tab.url.startsWith("chrome://")) {
        chrome.tabs.sendMessage(tab.id, {action: "trigger_crosshair"}).catch(()=>console.log("Tab not ready."));
    }
});

// 3. Take the Screenshot
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
    if (request.action === "take_screenshot") {
        chrome.tabs.captureVisibleTab(null, {format: "png"}, (dataUrl) => {
            sendResponse({screenshotUrl: dataUrl});
        });
        return true; 
    }
});
// --- 4. The Auth Bridge ---
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
    if (request.action === "get_auth_token") {
        // 1. First, check your live Vercel website for the token
        chrome.cookies.get({ url: 'https://kogvio-landing-page.vercel.app', name: '__session' }, (cookie) => {
            if (cookie) {
                sendResponse({ token: cookie.value });
            } else {
                // 2. Fallback to localhost just in case you are testing locally
                chrome.cookies.get({ url: 'http://127.0.0.1:5500', name: '__session' }, (cookie2) => {
                    if (cookie2) {
                        sendResponse({ token: cookie2.value });
                    } else {
                        sendResponse({ token: null });
                    }
                });
            }
        });
        return true; 
    }
});