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

// All three plan variants must render without error
for (const plan of ['free', 'professional', 'founding']) {
  test(`Checkout loads for plan=${plan}`, async ({ page }) => {
    const r = await page.goto(`/checkout?plan=${plan}`);
    expect(r?.status()).toBe(200);
    await expect(page.locator('body')).not.toBeEmpty();
    // Title must be set (not empty)
    const title = await page.title();
    expect(title.length).toBeGreaterThan(0);
  });
}

test('Checkout professional — has "Professional" plan name in body', async ({ page }) => {
  await page.goto('/checkout?plan=professional');
  await expect(page.locator('body')).toContainText('Professional');
});

test('Checkout founding — has "Founding" plan name in body', async ({ page }) => {
  await page.goto('/checkout?plan=founding');
  await expect(page.locator('body')).toContainText('Founding');
});

test('Checkout free — mentions free or sign up in body', async ({ page }) => {
  await page.goto('/checkout?plan=free');
  const body = await page.locator('body').textContent();
  expect(body?.toLowerCase()).toMatch(/free|sign|start|account/);
});

test('Checkout professional — auth section is present (sign in / create account)', async ({ page }) => {
  await page.goto('/checkout?plan=professional');
  // auth-section div is present in the DOM (may be hidden via CSS but rendered)
  await expect(page.locator('#auth-section')).toBeAttached();
  // Payment form is present
  await expect(page.locator('#payment-form')).toBeAttached();
});

test('Checkout professional — payment element container is present', async ({ page }) => {
  await page.goto('/checkout?plan=professional');
  await expect(page.locator('#payment-element')).toBeAttached();
});

test('Checkout — nav has back link to homepage', async ({ page }) => {
  await page.goto('/checkout?plan=professional');
  // The checkout nav has both a logo link and an explicit "← Back" nav-link,
  // both pointing to /. Use first() to avoid strict-mode violation.
  const navLink = page.locator('nav a[href="/"]').first();
  await expect(navLink).toBeAttached();
});

test('Checkout professional — canonical is production URL', async ({ page }) => {
  await page.goto('/checkout?plan=professional');
  const canonical = await page.locator('link[rel="canonical"]').getAttribute('href');
  expect(canonical).toContain('neurontechnologies.ai');
  expect(canonical).not.toContain('run.app');
  expect(canonical).not.toContain('stage');
});
