import { test, expect } from '@playwright/test';
import { execSync } from 'child_process'; // Für artisan seed

test.describe('Marketing Package Flow: Auswahl → Registrierung → Kauf (Free & Paid)', () => {
  test.beforeAll(async () => {
    // Seed Test-Tenant (einmalig)
    execSync('php artisan tenant:add-dummy --email=test@example.com --password=password123 --first_name=Test --last_name=User --address="Teststr. 1" --phone="+49123"');
    // Mock Verifizierung: Update DB (in Test-Env)
    execSync('php artisan tinker --execute="App\\Models\\User::where(\'email\', \'test@example.com\')->update([\'email_verified_at\' => now()]);"');
  });

  test('Free-Paket-Flow mit Wizard (ID=1, Starter, eingeloggter User)', async ({ page }) => {
    // Login first
    await page.goto('http://localhost:8000/de/login');
    await page.fill('[name="email"]', 'test@example.com');
    await page.fill('[name="password"]', 'password123');
    await page.getByRole('button', { name: 'Anmelden' }).click();
    await expect(page).toHaveURL(/\/dashboard/);

    // Go to Wizard
    await page.goto('http://localhost:8000/purchase-wizard/10');
    await expect(page.locator('text=Sie sind bereits eingeloggt')).toBeVisible();
    await page.getByRole('button', { name: 'Weiter zum Zahlungsschritt' }).click();
    await expect(page).toHaveURL(/\/purchase-wizard\/1/); // Next step
    await page.screenshot({ path: 'wizard-logged-in.png', fullPage: true });

    // Payment (Free: Success)
    await expect(page.locator('text=Free package assigned')).toBeVisible();
    await page.screenshot({ path: 'wizard-free-success.png', fullPage: true });
  });

  test('Wizard Login-Fehler mit Toast', async ({ page }) => {
    await page.goto('http://localhost:8000/purchase-wizard/10');
    // Switch to Login
    await page.getByRole('button', { name: 'Anmelden' }).click();
    await page.fill('[name="email"]', 'wrong@example.com');
    await page.fill('[name="password"]', 'wrong');
    await page.getByRole('button', { name: 'Anmelden' }).click();
    await expect(page.locator('[data-testid="toast"]')).toBeVisible(); // Toast for error
    await expect(page.locator('text=Ungültige Anmeldedaten')).toBeVisible(); // Inline error
    await page.screenshot({ path: 'wizard-login-error.png', fullPage: true });
  });

  test('Wizard Registrierung-Fehler mit Toast', async ({ page }) => {
    await page.goto('http://localhost:8000/purchase-wizard/10');
    // Reg form with invalid data
    await page.fill('[name="email"]', 'invalid');
    await page.getByRole('button', { name: 'Registrieren' }).click();
    await expect(page.locator('[data-testid="toast"]')).toBeVisible();
    await expect(page.locator('text=Das E-Mail muss eine gültige E-Mail-Adresse sein')).toBeVisible();
    await page.screenshot({ path: 'wizard-reg-error.png', fullPage: true });
  });

  test('Wizard Erfolgreiche Reg mit Success-Message', async ({ page }) => {
    await page.goto('http://localhost:8000/purchase-wizard/10');
    // Fill valid reg data (use unique email)
    await page.fill('[name="first_name"]', 'TestReg');
    await page.fill('[name="last_name"]', 'User');
    await page.fill('[name="email"]', 'testreg@example.com');
    await page.fill('[name="username"]', 'testreguser');
    await page.fill('[name="address"]', 'Teststr. 1');
    await page.fill('[name="phone"]', '+49123');
    await page.fill('[name="password"]', 'Password123!');
    await page.fill('[name="password_confirmation"]', 'Password123!');
    await page.check('[name="privacy_consent"]');
    await page.getByRole('button', { name: 'Registrieren' }).click();
    await expect(page.locator('text=Sie sind nun eingeloggt')).toBeVisible(); // Success message
    await page.waitForTimeout(2000); // Auto-next
    await expect(page).toHaveURL(/\/purchase-wizard\/1/); // Payment step
    await page.screenshot({ path: 'wizard-reg-success.png', fullPage: true });
  });

  test('Paid-Paket-Flow (ID=2, Pro mit Stripe-Test)', async ({ page }) => {
    // Ähnlich wie Free, aber package_id=2
    await page.goto('http://localhost:8000/de/packages');
    await page.getByRole('button', { name: 'Details anzeigen' }).nth(1).click(); // Zweites Paket (Paid)
    // ... (Modal, Register/Login wie oben)
    await expect(page).toHaveURL(/\/buy-packages\/2/);

    // Mock Stripe
    await page.route('https://checkout.stripe.com/**', async route => {
      await route.fulfill({ status: 200, body: '<html>Mock Stripe Success</html>' });
    });
    // Simuliere Checkout: Fill Test-Karte
    await page.fill('[name="cardNumber"]', '4242424242424242');
    await page.fill('[name="cardExpiry"]', '12/25');
    await page.fill('[name="cardCvc"]', '123');
    await page.click('[name="submit"]');
    await page.waitForURL(/\/marketing\/success/); // Nach Webhook
    await page.screenshot({ path: 'paid-step6-success.png', fullPage: true });

    // Integration: Limits-Check wie in package-flow.test.ts
    await expect(page.locator('text=Remaining Photos')).toContainText('Unbegrenzt'); // Pro-Limit
  });
});