const http = require('http');

const agentId = process.env.PAPERCLIP_AGENT_ID;
const apiKey = process.env.PAPERCLIP_API_KEY;
const apiUrl = process.env.PAPERCLIP_API_URL;
const runId = process.env.PAPERCLIP_RUN_ID;

console.log('Agent ID:', agentId);
console.log('API URL:', apiUrl);
console.log('Run ID:', runId);

if (!apiKey || !apiUrl) {
  console.error('Missing environment variables');
  process.exit(1);
}

async function fetchJson(url, options = {}) {
  const request = http.request({
    hostname: new URL(url).hostname,
    port: new URL(url).port,
    path: new URL(url).pathname,
    method: options.method || 'GET',
    headers: {
      'Authorization': `Bearer ${apiKey}`,
      'X-Paperclip-Run-Id': runId,
      ...options.headers
    }
  }, (response) => {
    let data = '';
    response.on('data', chunk => data += chunk);
    response.on('end', () => {
      try {
        console.log(JSON.stringify(JSON.parse(data), null, 2));
      } catch (e) {
        console.log(data);
      }
    });
  });
  request.on('error', console.error);
  request.end();
}

console.log('\n=== FETCHING AGENT IDENTITY ===\n');
fetchJson(`${apiUrl}/api/agents/me`).catch(console.error);

console.log('\n=== FETCHING INBOX-LITE ===\n');
fetchJson(`${apiUrl}/api/agents/me/inbox-lite`).catch(console.error);

console.log('\n=== FETCHING ALL ASSIGNED ISSUES ===\n');
fetchJson(`${apiUrl}/api/companies/${apiKey.split('-')[0] || 'unknown'}/issues?assigneeAgentId=${agentId}&status=todo,in_progress,blocked`).catch(console.error);
