import { test, expect } from '@playwright/test';
import { ae_app_local_data_defaults } from './_helpers/ae_defaults';
import { testing_event_id, testing_account_id } from './_helpers/env';
import { attach_minimal_routes } from './_helpers/minimal_ae_api_mocks';

const event_id = testing_event_id;

test.describe('Event Presenter Page - smoke', () => {
  test.beforeEach(async ({ page }) => {
    page.on('pageerror', (err) => console.error(`BROWSER ERROR: ${err.message}`));
    page.on('console', (msg) => {
      if (msg.type() === 'error' || msg.type() === 'warning') console.error(`BROWSER [${msg.type().toUpperCase()}]: ${msg.text()}`);
    });

    await attach_minimal_routes(page, event_id);

    await page.addInitScript(([defaults, event_id, account_id]) => {
      const test_data = { ...defaults, account_id: account_id, manager_access: true } as any;
      test_data.mod = { ...defaults.mod, events: { ...defaults.mod.events, event_id } };
      window.localStorage.setItem('ae_loc', JSON.stringify(test_data));
    }, [ae_app_local_data_defaults, event_id, testing_account_id] as const);
  });

  test('loads presenter page without console errors', async ({ page }) => {
    const presenter_id = 'pres-1';
    const errors: string[] = [];
    page.on('pageerror', (err) => errors.push(err.message));

    await page.goto(`/events/${event_id}/presenter/${presenter_id}`);
    await page.waitForResponse((r) => r.url().includes('site_domain/search') && r.status() === 200);

    expect(errors).toHaveLength(0);
  });
});

