import { test, expect } from '@playwright/test';

const PROXY_URL = process.env.PROXY_URL || 'http://localhost:4000';
const API_KEY = process.env.TEST_API_KEY || '';

test.describe('Journey 7: Proxy API Endpoints', () => {
  test.beforeEach(() => {
    test.skip(!process.env.PROXY_URL, 'requires live proxy (set PROXY_URL)');
  });

  test('GET /health returns 200', async ({ request }) => {
    const res = await request.get(`${PROXY_URL}/health`);
    expect(res.status()).toBe(200);
    const body = await res.json();
    expect(body).toHaveProperty('status');
  });

  test('GET /v1/models without auth returns 401', async ({ request }) => {
    const res = await request.get(`${PROXY_URL}/v1/models`);
    expect(res.status()).toBe(401);
  });

  test('GET /v1/models with Bearer key returns 200 and models array', async ({ request }) => {
    test.skip(!API_KEY, 'requires TEST_API_KEY');

    const res = await request.get(`${PROXY_URL}/v1/models`, {
      headers: { Authorization: `Bearer ${API_KEY}` },
    });

    expect(res.status()).toBe(200);
    const body = await res.json();
    expect(body).toHaveProperty('data');
    expect(Array.isArray(body.data)).toBe(true);
  });

  test('POST /v1/chat/completions non-stream returns choices', async ({ request }) => {
    test.skip(!API_KEY, 'requires TEST_API_KEY');

    const res = await request.post(`${PROXY_URL}/v1/chat/completions`, {
      headers: {
        Authorization: `Bearer ${API_KEY}`,
        'Content-Type': 'application/json',
      },
      data: {
        model: 'auto',
        messages: [{ role: 'user', content: 'Say hello in one word.' }],
        stream: false,
        max_tokens: 16,
      },
    });

    expect(res.status()).toBe(200);
    const body = await res.json();
    expect(body).toHaveProperty('choices');
    expect(Array.isArray(body.choices)).toBe(true);
    expect(body.choices.length).toBeGreaterThan(0);
  });

  test('POST /v1/chat/completions stream returns SSE chunks', async ({ request }) => {
    test.skip(!API_KEY, 'requires TEST_API_KEY');

    const res = await request.post(`${PROXY_URL}/v1/chat/completions`, {
      headers: {
        Authorization: `Bearer ${API_KEY}`,
        'Content-Type': 'application/json',
      },
      data: {
        model: 'auto',
        messages: [{ role: 'user', content: 'Say hi.' }],
        stream: true,
        max_tokens: 16,
      },
    });

    expect(res.status()).toBe(200);
    const contentType = res.headers()['content-type'] || '';
    expect(contentType).toContain('text/event-stream');

    const text = await res.text();
    expect(text).toContain('data: ');
  });

  test('Response headers include X-Request-ID', async ({ request }) => {
    test.skip(!API_KEY, 'requires TEST_API_KEY');

    const res = await request.get(`${PROXY_URL}/v1/models`, {
      headers: { Authorization: `Bearer ${API_KEY}` },
    });

    expect(res.status()).toBe(200);
    const requestId = res.headers()['x-request-id'];
    expect(requestId).toBeTruthy();
  });
});
