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

const mockDashboardStats = {
  totalUsers: 1250,
  newUsersLast30Days: 85,
  totalListings: 3400,
  newListingsLast30Days: 320,
  activeListings: 2800,
  pendingModeration: 45,
  totalAgents: 180,
  verifiedAgents: 120,
  totalTransactions: 560,
};

const mockRevenue = {
  data: [
    { period: '2025-10', totalRevenue: 150000000, subscriptionRevenue: 100000000, transactionRevenue: 50000000 },
    { period: '2025-11', totalRevenue: 180000000, subscriptionRevenue: 120000000, transactionRevenue: 60000000 },
    { period: '2025-12', totalRevenue: 200000000, subscriptionRevenue: 130000000, transactionRevenue: 70000000 },
    { period: '2026-01', totalRevenue: 220000000, subscriptionRevenue: 140000000, transactionRevenue: 80000000 },
    { period: '2026-02', totalRevenue: 250000000, subscriptionRevenue: 160000000, transactionRevenue: 90000000 },
    { period: '2026-03', totalRevenue: 280000000, subscriptionRevenue: 180000000, transactionRevenue: 100000000 },
  ],
};

test.describe('Admin Dashboard', () => {
  test.beforeEach(async ({ page }) => {
    await page.route('**/admin/dashboard**', (route) =>
      route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(mockDashboardStats) }),
    );
    await page.route('**/admin/revenue**', (route) =>
      route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(mockRevenue) }),
    );
  });

  test('renders admin dashboard with stat cards', async ({ page }) => {
    await page.goto('/admin');

    // Stat values should be visible
    await expect(page.getByText('1.250')).toBeVisible({ timeout: 10000 });
    await expect(page.getByText('3.400')).toBeVisible();
  });

  test('shows refresh button', async ({ page }) => {
    await page.goto('/admin');

    const refreshButton = page.getByRole('button').filter({ has: page.locator('svg') }).first();
    await expect(refreshButton).toBeVisible({ timeout: 10000 });
  });

  test('handles API failure gracefully', async ({ page }) => {
    await page.route('**/admin/dashboard**', (route) =>
      route.fulfill({ status: 500, body: 'Error' }),
    );

    await page.goto('/admin');

    // Page should still render without crashing
    await page.waitForTimeout(2000);
    await expect(page.locator('body')).toBeVisible();
  });
});
