/**
 * Journey 2: Stripe Checkout, Billing & Plan Management
 *
 * Tests Stripe integration endpoints and billing UI.
 * Works without real Stripe keys (graceful degradation).
 */

import { test, expect } from '@playwright/test';
import {
  loginSeedUser,
  takeScreenshot,
  setupConsoleErrorTracking,
} from './utils/test-helpers';

test.describe('Journey 2: Stripe Checkout & Billing', () => {
  let consoleErrors: string[];

  test.beforeEach(async ({ page }) => {
    consoleErrors = setupConsoleErrorTracking(page);
  });

  test('should display plan info on settings page', async ({ page }) => {
    await loginSeedUser(page);
    await page.goto('/dashboard/settings');
    await page.waitForTimeout(1000);
    await takeScreenshot(page, 'stripe-01-settings-page');

    // Settings page should load without errors
    const pageContent = await page.textContent('body');
    expect(pageContent).toBeTruthy();
    expect(consoleErrors).toHaveLength(0);
  });

  test('should test checkout API endpoint', async ({ page }) => {
    await loginSeedUser(page);
    await page.goto('/dashboard/settings');

    const response = await page.request.post('/api/stripe/checkout', {
      data: { priceId: 'price_test_123' },
    });

    // Possible states:
    // - 503: Stripe not configured (no keys)
    // - 500: Stripe configured but invalid price ID (expected for test)
    // - 200: Stripe configured with a valid price ID
    const status = response.status();
    expect([200, 400, 500, 503]).toContain(status);

    if (status === 503) {
      const data = await response.json();
      expect(data.error).toContain('Stripe is not configured');
    } else if (status === 200) {
      const data = await response.json();
      expect(data.url).toBeTruthy();
    }

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

  test('should test billing portal API endpoint', async ({ page }) => {
    await loginSeedUser(page);
    await page.goto('/dashboard/settings');

    const response = await page.request.post('/api/stripe/portal');

    // Possible states:
    // - 400: User has no Stripe customer (free plan, never subscribed)
    // - 503: Stripe not configured
    // - 200: Stripe configured and user has a customer ID
    // - 500: Stripe configured but API error
    const status = response.status();
    expect([200, 400, 500, 503]).toContain(status);

    if (status === 503) {
      const data = await response.json();
      expect(data.error).toContain('Stripe is not configured');
    } else if (status === 400) {
      const data = await response.json();
      expect(data.error).toContain('No billing account found');
    } else if (status === 200) {
      const data = await response.json();
      expect(data.url).toBeTruthy();
    }

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

  test('should reject unauthorized checkout access', async ({ page }) => {
    // No login — direct API call
    const response = await page.request.post('/api/stripe/checkout', {
      data: { priceId: 'price_test_123' },
    });

    expect(response.status()).toBe(401);
    const data = await response.json();
    expect(data.error).toBe('Unauthorized');
  });

  test('should reject unauthorized portal access', async ({ page }) => {
    // No login — direct API call
    const response = await page.request.post('/api/stripe/portal');

    expect(response.status()).toBe(401);
    const data = await response.json();
    expect(data.error).toBe('Unauthorized');
  });

  test('should validate missing price ID on checkout', async ({ page }) => {
    await loginSeedUser(page);
    await page.goto('/dashboard/settings');

    const response = await page.request.post('/api/stripe/checkout', {
      data: {},
    });

    // Either 400 (validation) or 503 (Stripe not configured)
    expect([400, 503]).toContain(response.status());
    expect(consoleErrors).toHaveLength(0);
  });
});
