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

const mockModerationQueue = {
  data: [
    {
      id: 'mod-1', listingId: 'l1', title: 'Căn hộ cần duyệt', propertyType: 'APARTMENT',
      transactionType: 'SALE', price: 5000000000, sellerName: 'Nguyen Van A',
      aiModerationScore: 85, submittedAt: '2026-03-01T00:00:00Z', status: 'PENDING',
    },
    {
      id: 'mod-2', listingId: 'l2', title: 'Nhà phố cần duyệt', propertyType: 'HOUSE',
      transactionType: 'RENT', price: 15000000, sellerName: 'Tran Thi B',
      aiModerationScore: 42, submittedAt: '2026-03-02T00:00:00Z', status: 'PENDING',
    },
  ],
  total: 2, page: 1, limit: 20, totalPages: 1,
};

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

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

    await expect(page.getByText('Căn hộ cần duyệt')).toBeVisible({ timeout: 10000 });
    await expect(page.getByText('Nhà phố cần duyệt')).toBeVisible();
  });

  test('displays AI moderation scores', async ({ page }) => {
    await page.goto('/admin/moderation');

    await expect(page.getByText('Căn hộ cần duyệt')).toBeVisible({ timeout: 10000 });
    await expect(page.getByText('85')).toBeVisible();
    await expect(page.getByText('42')).toBeVisible();
  });

  test('shows seller names', async ({ page }) => {
    await page.goto('/admin/moderation');

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

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

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

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

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