import { test, expect } from '@playwright/test';
import { login, PORT_SLUG } from './helpers';

test.describe('Receipt scanner PWA (Phase B)', () => {
  test.beforeEach(async ({ page }) => {
    await login(page, 'super_admin');
  });

  test('scanner page loads with capture button and no dashboard chrome', async ({ page }) => {
    // First-hit dev compile of the brand-new (scanner) route group can exceed 30s.
    await page.goto(`/${PORT_SLUG}/scan`, { timeout: 60_000 });
    await expect(page.getByRole('heading', { name: /scan a receipt/i })).toBeVisible({
      timeout: 20_000,
    });
    await expect(page.getByTestId('scan-capture')).toBeVisible({ timeout: 10_000 });

    // No sidebar / topbar elements should be present on the scanner.
    const dashboardLinks = page.getByRole('link', { name: 'Dashboard' });
    expect(await dashboardLinks.count()).toBe(0);
  });

  test('per-port manifest is served with correct content type and scope', async ({ page }) => {
    const res = await page.request.get(`/${PORT_SLUG}/scan/manifest.webmanifest`, {
      timeout: 60_000,
    });
    expect(res.status()).toBe(200);
    expect(res.headers()['content-type']).toContain('application/manifest+json');
    const body = (await res.json()) as {
      name: string;
      scope: string;
      start_url: string;
      display: string;
    };
    expect(body.scope).toBe(`/${PORT_SLUG}/scan`);
    expect(body.start_url).toBe(`/${PORT_SLUG}/scan`);
    expect(body.display).toBe('standalone');
    expect(body.name.toLowerCase()).toContain('scanner');
  });

  test('admin OCR settings page renders both provider and model selectors', async ({ page }) => {
    await page.goto(`/${PORT_SLUG}/admin/ocr`, { timeout: 60_000 });
    await expect(page.getByRole('heading', { name: /receipt ocr/i })).toBeVisible({
      timeout: 30_000,
    });
    await expect(page.getByTestId('save-port')).toBeVisible({ timeout: 15_000 });
    // Provider + model selects use Radix triggers, not native <label>; locate via testid.
    await expect(page.locator('#provider-port')).toBeVisible();
    await expect(page.locator('#model-port')).toBeVisible();
  });
});
