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

type AuthFixtures = {
  adminToken: string;
  viewerToken: string;
};

export const test = base.extend<AuthFixtures>({
  adminToken: async ({ request }, use) => {
    const res = await request.post('/api/auth/login', {
      data: {
        username: process.env.TEST_USER_ADMIN ?? 'admin',
        password: process.env.TEST_USER_ADMIN_PASS ?? 'admin123',
      },
    });
    expect(res.status()).toBe(200);
    const { token } = await res.json();
    await use(token);
  },

  viewerToken: async ({ request }, use) => {
    const res = await request.post('/api/auth/login', {
      data: {
        username: process.env.TEST_USER_VIEWER ?? 'viewer',
        password: process.env.TEST_USER_VIEWER_PASS ?? 'viewer123',
      },
    });
    expect(res.status()).toBe(200);
    const { token } = await res.json();
    await use(token);
  },
});

export { expect };
