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

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

const superAdminTokens = getCachedTokens('superAdmin');
const trainerTokens = getCachedTokens('trainer');
const clubAdminTokens = getCachedTokens('clubAdmin');
const coordinatorTokens = getCachedTokens('coordinator');

test.describe('Admin Clubs API', () => {
  test('GET /v1/clubs — returns paginated clubs for super_admin', async ({ request }) => {
    const response = await request.get(`${API_URL}/v1/clubs`, {
      headers: authHeaders(superAdminTokens),
    });

    expect(response.ok()).toBeTruthy();
    const body = await response.json();
    expect(body.data).toBeDefined();
    expect(Array.isArray(body.data)).toBe(true);
    expect(body).toHaveProperty('hasMore');
    expect(body).toHaveProperty('nextCursor');
  });

  test('GET /v1/clubs?limit=100 — respects max limit', async ({ request }) => {
    const response = await request.get(`${API_URL}/v1/clubs?limit=100`, {
      headers: authHeaders(superAdminTokens),
    });

    expect(response.ok()).toBeTruthy();
    const body = await response.json();
    expect(body.data.length).toBeLessThanOrEqual(100);
  });

  test('GET /v1/clubs?limit=101 — returns 400 for limit > 100', async ({ request }) => {
    const response = await request.get(`${API_URL}/v1/clubs?limit=101`, {
      headers: authHeaders(superAdminTokens),
    });

    expect(response.status()).toBe(400);
    const body = await response.json();
    expect(body.message).toBeDefined();
  });

  test('GET /v1/clubs?limit=500 — returns 400 for limit=500', async ({ request }) => {
    const response = await request.get(`${API_URL}/v1/clubs?limit=500`, {
      headers: authHeaders(superAdminTokens),
    });

    expect(response.status()).toBe(400);
  });

  test('GET /v1/clubs?limit=0 — returns 400 for limit < 1', async ({ request }) => {
    const response = await request.get(`${API_URL}/v1/clubs?limit=0`, {
      headers: authHeaders(superAdminTokens),
    });

    expect(response.status()).toBe(400);
  });

  test('GET /v1/clubs — non-admin gets 403', async ({ request }) => {
    const response = await request.get(`${API_URL}/v1/clubs`, {
      headers: authHeaders(trainerTokens),
    });

    expect(response.status()).toBe(403);
  });

  test('GET /v1/clubs — club_admin gets 403', async ({ request }) => {
    const response = await request.get(`${API_URL}/v1/clubs`, {
      headers: authHeaders(clubAdminTokens),
    });

    expect(response.status()).toBe(403);
  });

  test('GET /v1/clubs?search=FitGym — search filter works', async ({ request }) => {
    const response = await request.get(`${API_URL}/v1/clubs?search=FitGym`, {
      headers: authHeaders(superAdminTokens),
    });

    expect(response.ok()).toBeTruthy();
    const body = await response.json();
    expect(body.data.length).toBeGreaterThanOrEqual(1);
    expect(body.data[0].name).toContain('FitGym');
  });

  test('GET /v1/clubs — response includes license and _count', async ({ request }) => {
    const response = await request.get(`${API_URL}/v1/clubs`, {
      headers: authHeaders(superAdminTokens),
    });

    expect(response.ok()).toBeTruthy();
    const body = await response.json();
    expect(body.data.length).toBeGreaterThanOrEqual(1);

    const club = body.data[0];
    expect(club).toHaveProperty('id');
    expect(club).toHaveProperty('name');
    expect(club).toHaveProperty('isActive');
    expect(club).toHaveProperty('license');
    expect(club).toHaveProperty('_count');
    expect(club._count).toHaveProperty('users');
    expect(club._count).toHaveProperty('clients');
  });
});

test.describe('Admin Audit Logs API', () => {
  test('GET /v1/admin/audit-logs — returns logs for super_admin', async ({ request }) => {
    const response = await request.get(`${API_URL}/v1/admin/audit-logs`, {
      headers: authHeaders(superAdminTokens),
    });

    expect(response.ok()).toBeTruthy();
    const body = await response.json();
    expect(body).toHaveProperty('data');
    expect(Array.isArray(body.data)).toBe(true);
  });

  test('GET /v1/admin/audit-logs — trainer gets 403', async ({ request }) => {
    const response = await request.get(`${API_URL}/v1/admin/audit-logs`, {
      headers: authHeaders(trainerTokens),
    });

    expect(response.status()).toBe(403);
  });
});

test.describe('Pagination Limits', () => {
  test('GET /v1/clients?limit=101 — returns 400', async ({ request }) => {
    const response = await request.get(`${API_URL}/v1/clients?limit=101`, {
      headers: authHeaders(coordinatorTokens),
    });

    expect(response.status()).toBe(400);
  });

  test('GET /v1/trainings?limit=200 — returns 400', async ({ request }) => {
    const response = await request.get(`${API_URL}/v1/trainings?limit=200`, {
      headers: authHeaders(trainerTokens),
    });

    expect(response.status()).toBe(400);
  });

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

    expect(response.status()).toBe(400);
  });

  test('GET /v1/clients?limit=-1 — returns 400', async ({ request }) => {
    const response = await request.get(`${API_URL}/v1/clients?limit=-1`, {
      headers: authHeaders(coordinatorTokens),
    });

    expect(response.status()).toBe(400);
  });

  test('GET /v1/clients?limit=abc — returns 400 for non-numeric', async ({ request }) => {
    const response = await request.get(`${API_URL}/v1/clients?limit=abc`, {
      headers: authHeaders(coordinatorTokens),
    });

    expect(response.status()).toBe(400);
  });
});
