import { test, expect } from '@playwright/test';
import { getCachedTokens, authHeaders } from './helpers';

const API_URL = process.env.E2E_API_URL || 'http://localhost:3000';

const trainerTokens = getCachedTokens('trainer');

test.describe('Funnel API', () => {
  test('GET /v1/funnel — returns funnel entries', async ({ request }) => {
    const response = await request.get(`${API_URL}/v1/funnel`, {
      headers: authHeaders(trainerTokens),
    });

    expect(response.ok()).toBeTruthy();
    const body = await response.json();
    expect(body.data).toBeInstanceOf(Array);
  });

  test('GET /v1/funnel?stage=NEW — filters by stage', async ({ request }) => {
    const response = await request.get(`${API_URL}/v1/funnel?stage=NEW`, {
      headers: authHeaders(trainerTokens),
    });

    expect(response.ok()).toBeTruthy();
    const body = await response.json();
    expect(body.data).toBeInstanceOf(Array);
  });

  test('GET /v1/stats/funnel — returns conversion stats', async ({ request }) => {
    const response = await request.get(`${API_URL}/v1/stats/funnel`, {
      headers: authHeaders(trainerTokens),
    });

    expect(response.ok()).toBeTruthy();
    const body = await response.json();
    expect(body).toHaveProperty('newToAssigned');
    expect(body).toHaveProperty('assignedToCompleted');
    expect(body).toHaveProperty('completedToRegular');
  });
});
