import { test, expect } from '@playwright/test';
import { queryDb } from './helpers/db';

test.describe('Landing page', () => {
  test('hero section renders with email capture form', async ({ page }) => {
    await page.goto('/');

    await expect(page.getByRole('heading', { level: 1 })).toContainText('AI Image Generation');
    const heroForm = page.locator('#get-access');
    await expect(heroForm.getByPlaceholder(/email/i)).toBeVisible();
    await expect(heroForm.getByRole('button', { name: /early access/i })).toBeVisible();

    await page.screenshot({ path: 'test-results/screenshots/landing-hero.png', fullPage: true });
  });

  test('page loads without console errors', async ({ page }) => {
    const errors: string[] = [];
    page.on('console', (msg) => {
      if (msg.type() === 'error') errors.push(msg.text());
    });

    await page.goto('/');
    await page.waitForLoadState('networkidle');

    expect(errors).toEqual([]);
  });
});

test.describe('Database cross-check', () => {
  test('dev postgres is reachable and has expected tables', async () => {
    const tables = queryDb<{ table_name: string }>(
      "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY table_name",
    );
    const names = tables.map((t) => t.table_name);

    expect(names).toContain('api_keys');
  });
});
