const fs = require('fs');
const path = require('path');
const https = require('https');
const http = require('http');

// Read .env manually (same as video-api-adapter.js)
const envPath = path.resolve(__dirname, '../.env');
const envContent = fs.readFileSync(envPath, 'utf-8');
const env = {};
envContent.split('\n').forEach(line => {
  const trimmed = line.trim();
  if (trimmed && !trimmed.startsWith('#')) {
    const [key, ...vals] = trimmed.split('=');
    if (key && vals.length) env[key.trim()] = vals.join('=').trim();
  }
});

const API_KEY = env.JIMENG_API_KEY;
const BASE_URL = env.JIMENG_BASE_URL || 'https://ark.cn-beijing.volces.com/api/v3';
const TASK_ID = 'cgt-20260612215215-sb2z2';

function httpGet(url) {
  const urlObj = new URL(url);
  const transport = urlObj.protocol === 'https:' ? https : http;
  return new Promise((resolve, reject) => {
    const req = transport.request(url, {
      method: 'GET',
      headers: { 'Authorization': `Bearer ${API_KEY}`, 'Content-Type': 'application/json' },
      timeout: 30000,
    }, (res) => {
      let data = '';
      res.on('data', chunk => data += chunk);
      res.on('end', () => {
        try { resolve(JSON.parse(data)); }
        catch(e) { resolve({ _raw: data }); }
      });
    });
    req.on('error', reject);
    req.end();
  });
}

async function main() {
  console.log('Task ID:', TASK_ID);
  const data = await httpGet(`${BASE_URL}/contents/generations/tasks/${TASK_ID}`);
  console.log(JSON.stringify(data, null, 2));
}

main().catch(err => console.error(err));
