/**
 * Test script for bot actions
 * Usage: node test-bot-action.js <roomCode> <action>
 * 
 * Examples:
 *   node test-bot-action.js ABC123 '{"action":"draw"}'
 *   node test-bot-action.js ABC123 '{"action":"play","card":"4R"}'
 *   node test-bot-action.js ABC123 '{"action":"play","card":"W","color":"R"}'
 *   node test-bot-action.js ABC123 '{"action":"uno"}'
 */

const http = require('http');

const roomCode = process.argv[2];
const actionJson = process.argv[3];

if (!roomCode || !actionJson) {
    console.error('❌ Usage: node test-bot-action.js <roomCode> <actionJson>');
    console.error('Example: node test-bot-action.js ABC123 \'{"action":"draw"}\'');
    process.exit(1);
}

let action;
try {
    action = JSON.parse(actionJson);
} catch (e) {
    console.error('❌ Invalid JSON:', e.message);
    process.exit(1);
}

console.log(`🤖 Sending bot action to room ${roomCode}:`);
console.log(JSON.stringify(action, null, 2));

const postData = JSON.stringify(action);

const options = {
    hostname: 'localhost',
    port: 5000,
    path: `/api/game/${roomCode}/action`,
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Content-Length': Buffer.byteLength(postData)
    }
};

const req = http.request(options, (res) => {
    let data = '';
    
    res.on('data', (chunk) => {
        data += chunk;
    });
    
    res.on('end', () => {
        console.log(`\n📡 Response (${res.statusCode}):`);
        try {
            const response = JSON.parse(data);
            console.log(JSON.stringify(response, null, 2));
            if (response.success) {
                console.log('✅ Action sent successfully!');
            } else {
                console.log('❌ Action failed');
            }
        } catch (e) {
            console.log(data);
        }
    });
});

req.on('error', (e) => {
    console.error('❌ Request failed:', e.message);
    console.error('Make sure the server is running on port 5000');
});

req.write(postData);
req.end();
