
import { MetaAIClient } from '../lib/providers/meta-client';
import fs from 'fs/promises';

// Using the same merged cookies as before
const cookies = JSON.stringify([
    // Facebook.com Cookies
    { "name": "ps_l", "value": "1", "domain": ".facebook.com" },
    { "name": "datr", "value": "jmoAaYIQoplmU7vRl4HLcWR1", "domain": ".facebook.com" },
    { "name": "fr", "value": "1gwnCVQoEw5LWJxUB.AWc1MjEm7uVeIdEsLn-TUeiaZxnLxSQbM7dDffWSsNOPIgWYNF8.BpW0HQ..AAA.0.0.BpW05z.AWcDBTaFWpJX3Z6ZQg75ZOXRuVQ", "domain": ".facebook.com" },
    { "name": "xs", "value": "37%3ADiIYP9JHKsgiSg%3A2%3A1766630179%3A-1%3A-1%3A%3AAcxje68BFTHTAbgeI7wPp81xw202cNOkodYrSaBkoDI", "domain": ".facebook.com" },
    { "name": "c_user", "value": "100003107523811", "domain": ".facebook.com" },
    { "name": "presence", "value": "C%7B%22t3%22%3A%5B%5D%2C%22utc3%22%3A1767591541854%2C%22v%22%3A1%7D", "domain": ".facebook.com" },
    { "name": "ar_debug", "value": "1", "domain": ".facebook.com" },
    { "name": "dpr", "value": "1", "domain": ".facebook.com" },
    { "name": "pas", "value": "100003107523811%3AUog8PlK0u2", "domain": ".facebook.com" },
    { "name": "ps_n", "value": "1", "domain": ".facebook.com" },
    { "name": "sb", "value": "jmoAaRKnokS_7bViVC_l7BA7", "domain": ".facebook.com" },
    { "name": "wl_cbv", "value": "v2%3Bclient_version%3A3004%3Btimestamp%3A1764632896", "domain": ".facebook.com" },
    // Meta.ai Cookies
    { "name": "abra_sess", "value": "Fqit0PrQ6JMDFg4YDmh1d096TE01eW8xTUhnFvyu15ANAA%3D%3D", "domain": ".meta.ai" }
]);

async function testMsg() {
    console.log("Initializing MetaAIClient...");
    const client = new MetaAIClient({ cookies });

    try {
        console.log("Sending simple text message 'Hello'...");
        await (client as any).initSession();
        console.log("Session State:", (client as any).session);

        // Manually calling sendPrompt which is private, by casting to any
        // Use a simple text prompt, not generating images
        const response = await (client as any).sendPrompt("Hello, are you working?");

        console.log("Raw Response:", JSON.stringify(response, null, 2));

        // Check for success indicators
        if (response?.data?.node?.bot_response_message || response?.data?.xabraAIPreviewMessageSendMutation?.message) {
            console.log("SUCCESS: Message sent and response received.");
        } else {
            console.log("FAILURE: Unexpected response structure.");
        }

    } catch (e: any) {
        console.error("Test Message Failed:", e.message);
        if (e.message.includes("HTML error")) {
            // If we get HTML error, maybe save it?
            console.log("Saving HTML error for inspection...");
            // This logic depends on throw in client, might not have access to raw text here easily unless we modify client again
        }
    }
}

testMsg();
