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

const mockValuationResult = {
  id: 'val-e2e-1',
  propertyType: 'APARTMENT',
  area: 75,
  district: 'Quận 1',
  city: 'Ho Chi Minh',
  estimatedPriceVND: 5_500_000_000,
  priceRangeLow: 5_100_000_000,
  priceRangeHigh: 5_900_000_000,
  pricePerM2: 73_333_333,
  confidence: 0.82,
  modelVersion: 'avm-v2.0',
  priceDrivers: [
    { feature: 'area_m2', impact: 24.5, direction: 'positive' as const },
    { feature: 'distance_to_cbd_km', impact: 12.3, direction: 'negative' as const },
  ],
  comparables: [
    {
      id: 'c1',
      listingId: 'l1',
      propertyType: 'APARTMENT',
      area: 72,
      district: 'Quận 1',
      pricePerM2: 74_500_000,
      priceVnd: 5_364_000_000,
      distanceKm: 0.8,
      publishedAt: '2026-03-12T00:00:00Z',
    },
  ],
  modelPredictions: [
    { modelName: 'xgboost', weight: 0.6, predictedPriceVnd: 5_500_000_000, predictedPricePerM2Vnd: 73_333_333 },
    { modelName: 'lightgbm', weight: 0.4, predictedPriceVnd: 5_480_000_000, predictedPricePerM2Vnd: 73_066_666 },
  ],
  ensembleMethod: 'weighted_average',
  createdAt: '2026-04-18T00:00:00Z',
};

const mockHistory = { data: [], total: 0, page: 1, totalPages: 1, limit: 10 };

async function setupMocks(page: import('@playwright/test').Page) {
  await page.route('**/auth/me', (route) =>
    route.fulfill({
      status: 200,
      contentType: 'application/json',
      body: JSON.stringify({ id: 'u1', email: 'e2e@test.vn', fullName: 'E2E User', role: 'USER' }),
    }),
  );
  await page.route('**/analytics/valuation/history**', (route) =>
    route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(mockHistory) }),
  );
  await page.route('**/analytics/valuation', (route) => {
    if (route.request().method() === 'POST') {
      return route.fulfill({
        status: 200,
        contentType: 'application/json',
        body: JSON.stringify(mockValuationResult),
      });
    }
    return route.continue();
  });
}

test.describe('AVM v2 Valuation Page', () => {
  test('submit form -> render result card with confidence + price range', async ({ page }) => {
    await setupMocks(page);
    await page.goto('/vi/dashboard/valuation');

    await page.locator('#propertyType').selectOption('APARTMENT');
    await page.locator('#district').fill('Quận 1');
    await page.locator('#area').fill('75');

    await page.getByRole('button', { name: /Định giá ngay/i }).click();

    const results = page.locator('#valuation-results');
    await expect(results).toBeVisible();
    await expect(results).toContainText('5.500.000.000');
    await expect(results).toContainText('Độ tin cậy cao');
    await expect(results).toContainText('avm-v2.0');
  });

  test('renders rate-limit error state on HTTP 429', async ({ page }) => {
    await page.route('**/auth/me', (route) =>
      route.fulfill({
        status: 200,
        contentType: 'application/json',
        body: JSON.stringify({ id: 'u1', email: 'e2e@test.vn', fullName: 'E2E User', role: 'USER' }),
      }),
    );
    await page.route('**/analytics/valuation/history**', (route) =>
      route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(mockHistory) }),
    );
    await page.route('**/analytics/valuation', (route) => {
      if (route.request().method() === 'POST') {
        return route.fulfill({
          status: 429,
          contentType: 'application/json',
          body: JSON.stringify({ message: 'Too many requests' }),
        });
      }
      return route.continue();
    });

    await page.goto('/vi/dashboard/valuation');
    await page.locator('#propertyType').selectOption('APARTMENT');
    await page.locator('#district').fill('Quận 1');
    await page.locator('#area').fill('75');
    await page.getByRole('button', { name: /Định giá ngay/i }).click();

    const alert = page.getByTestId('valuation-error');
    await expect(alert).toBeVisible();
    await expect(alert).toContainText('Quá nhiều yêu cầu');
  });

  test('export PDF button is visible after a successful valuation', async ({ page }) => {
    await setupMocks(page);
    await page.goto('/vi/dashboard/valuation');

    await page.locator('#propertyType').selectOption('APARTMENT');
    await page.locator('#district').fill('Quận 1');
    await page.locator('#area').fill('75');
    await page.getByRole('button', { name: /Định giá ngay/i }).click();

    await expect(page.locator('#valuation-results')).toBeVisible();
    // Export PDF button (uses Download icon + label)
    await expect(page.getByRole('button', { name: /Xuất PDF|Export PDF|Tải PDF/i })).toBeVisible();
  });
});
