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

test.describe('SmartAuth: Device Management', () => {
  test.beforeEach(async ({ page }) => {
    await page.goto('/login');
    await page.evaluate(() => {
      localStorage.setItem('admin_access_token', 'mock-token');
      localStorage.setItem('admin_refresh_token', 'mock-refresh');
      localStorage.setItem(
        'admin_auth_user',
        JSON.stringify({
          email: 'admin@example.com',
          name: 'Admin User',
          role: 'super_admin',
        })
      );
    });
  });

  test('device management page loads', async ({ page }) => {
    await page.route('**/api/auth/devices', async route => {
      if (route.request().method() === 'GET') {
        await route.fulfill({ status: 200, contentType: 'application/json', body: '[]' });
      }
    });
    await page.goto('/settings/devices');
    await expect(page.getByText('Device Management')).toBeVisible({ timeout: 10000 });
  });

  test('shows empty state when no devices', async ({ page }) => {
    await page.route('**/api/auth/devices', async route => {
      await route.fulfill({ status: 200, contentType: 'application/json', body: '[]' });
    });
    await page.goto('/settings/devices');
    await expect(page.getByText('No devices found')).toBeVisible({ timeout: 10000 });
  });

  test('renders device cards with trust badges', async ({ page }) => {
    await page.route('**/api/auth/devices', async route => {
      if (route.request().method() === 'GET') {
        await route.fulfill({
          status: 200,
          contentType: 'application/json',
          body: JSON.stringify([
            {
              id: 'dev-1',
              name: 'Chrome on macOS',
              platform: 'web-browser',
              trustLevel: 'trusted',
              trustExpiresAt: null,
              lastIp: '192.168.1.1',
              lastLoginAt: new Date().toISOString(),
              createdAt: new Date().toISOString(),
            },
            {
              id: 'dev-2',
              name: 'Safari on iPhone',
              platform: 'mobile-ios',
              trustLevel: 'remembered',
              trustExpiresAt: null,
              lastIp: '10.0.0.1',
              lastLoginAt: new Date().toISOString(),
              createdAt: new Date().toISOString(),
            },
          ]),
        });
      }
    });
    await page.goto('/settings/devices');
    await expect(page.getByText('Chrome on macOS')).toBeVisible({ timeout: 10000 });
    await expect(page.getByText('Safari on iPhone')).toBeVisible();
    await expect(page.getByText('Trusted', { exact: true })).toBeVisible();
    await expect(page.getByText('Remembered', { exact: true })).toBeVisible();
  });

  test('revoke all button appears with multiple devices', async ({ page }) => {
    await page.route('**/api/auth/devices', async route => {
      if (route.request().method() === 'GET') {
        await route.fulfill({
          status: 200,
          contentType: 'application/json',
          body: JSON.stringify([
            {
              id: 'dev-1',
              name: 'D1',
              platform: 'web',
              trustLevel: 'trusted',
              trustExpiresAt: null,
              lastIp: '1.1.1.1',
              lastLoginAt: new Date().toISOString(),
              createdAt: new Date().toISOString(),
            },
            {
              id: 'dev-2',
              name: 'D2',
              platform: 'web',
              trustLevel: 'unknown',
              trustExpiresAt: null,
              lastIp: '2.2.2.2',
              lastLoginAt: new Date().toISOString(),
              createdAt: new Date().toISOString(),
            },
          ]),
        });
      }
    });
    await page.goto('/settings/devices');
    await expect(page.getByRole('button', { name: /revoke all/i })).toBeVisible({ timeout: 10000 });
  });
});
