const http = require('http');

function makeRequest(method, path, data = null) {
  return new Promise((resolve, reject) => {
    const options = {
      hostname: 'localhost',
      port: 3000,
      path: path,
      method: method,
      headers: {
        'Content-Type': 'application/json'
      }
    };

    const req = http.request(options, (res) => {
      let body = '';
      res.on('data', (chunk) => { body += chunk; });
      res.on('end', () => {
        try {
          const json = JSON.parse(body);
          resolve({ status: res.statusCode, data: json });
        } catch (e) {
          resolve({ status: res.statusCode, data: body });
        }
      });
    });

    req.on('error', reject);
    
    if (data) {
      req.write(JSON.stringify(data));
    }
    req.end();
  });
}

async function testAPIs() {
  console.log('🧪 开始测试视频生成API\n');
  console.log('=' .repeat(50));

  // 测试1：获取项目列表
  console.log('\n1. 测试 GET /api/video/projects');
  try {
    const res1 = await makeRequest('GET', '/api/video/projects');
    console.log('状态码:', res1.status);
    console.log('响应:', JSON.stringify(res1.data, null, 2));
  } catch (e) {
    console.log('❌ 请求失败:', e.message);
  }

  // 测试2：创建项目
  console.log('\n2. 测试 POST /api/video/projects');
  try {
    const res2 = await makeRequest('POST', '/api/video/projects', {
      title: '测试视频项目',
      config: {
        video: { width: 720, height: 1280, fps: 30 },
        kenburns: { enabled: true, minZoom: 1.0, maxZoom: 1.2 }
      }
    });
    console.log('状态码:', res2.status);
    console.log('响应:', JSON.stringify(res2.data, null, 2));
  } catch (e) {
    console.log('❌ 请求失败:', e.message);
  }

  // 测试3：获取素材列表
  console.log('\n3. 测试 GET /api/video/materials');
  try {
    const res3 = await makeRequest('GET', '/api/video/materials');
    console.log('状态码:', res3.status);
    console.log('响应:', JSON.stringify(res3.data, null, 2));
  } catch (e) {
    console.log('❌ 请求失败:', e.message);
  }

  // 测试4：检查其他已知API
  console.log('\n4. 测试 GET /api/templates (验证其他API)');
  try {
    const res4 = await makeRequest('GET', '/api/templates');
    console.log('状态码:', res4.status);
    console.log('响应:', JSON.stringify(res4.data, null, 2));
  } catch (e) {
    console.log('❌ 请求失败:', e.message);
  }

  console.log('\n' + '=' .repeat(50));
  console.log('✅ 测试完成\n');
}

testAPIs().catch(console.error);
