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

const mockKycQueue = {
  data: [
    {
      id: 'kyc-1', userId: 'u1', fullName: 'Nguyen Van A', phone: '0912345678',
      email: 'a@test.com', role: 'AGENT', kycStatus: 'PENDING',
      submittedAt: '2026-03-01T00:00:00Z',
      kycData: { idType: 'CCCD', idNumber: '123456789012', frontImageUrl: '/id-front.jpg', backImageUrl: '/id-back.jpg', selfieUrl: '/selfie.jpg' },
    },
    {
      id: 'kyc-2', userId: 'u2', fullName: 'Tran Thi B', phone: '0987654321',
      email: null, role: 'AGENT', kycStatus: 'PENDING',
      submittedAt: '2026-03-02T00:00:00Z',
      kycData: { idType: 'PASSPORT', idNumber: 'B1234567' },
    },
  ],
  total: 2, page: 1, limit: 20, totalPages: 1,
};

test.describe('Admin KYC Page', () => {
  test.beforeEach(async ({ page }) => {
    await page.route('**/admin/kyc**', (route) => {
      if (route.request().method() === 'GET') {
        return route.fulfill({
          status: 200,
          contentType: 'application/json',
          body: JSON.stringify(mockKycQueue),
        });
      }
      return route.continue();
    });
  });

  test('renders KYC queue with applicants', async ({ page }) => {
    await page.goto('/admin/kyc');

    await expect(page.getByText('Nguyen Van A')).toBeVisible({ timeout: 10000 });
    await expect(page.getByText('Tran Thi B')).toBeVisible();
  });

  test('displays KYC status badges', async ({ page }) => {
    await page.goto('/admin/kyc');

    await expect(page.getByText('Nguyen Van A')).toBeVisible({ timeout: 10000 });
    // Should show pending status badges
    const pendingBadges = page.getByText(/Chờ duyệt|PENDING/i);
    await expect(pendingBadges.first()).toBeVisible();
  });

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

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

  test('handles empty KYC queue', async ({ page }) => {
    await page.route('**/admin/kyc**', (route) =>
      route.fulfill({
        status: 200,
        contentType: 'application/json',
        body: JSON.stringify({ data: [], total: 0, page: 1, limit: 20, totalPages: 0 }),
      }),
    );

    await page.goto('/admin/kyc');
    await page.waitForTimeout(2000);
    await expect(page.locator('body')).toBeVisible();
  });
});
