import { test, expectFixture as expect } from '../helpers/test-fixtures';

test.describe('Standard package checkout with coupons', () => {
  test('applies seeded coupon and shows discount summary', async ({
    page,
    tenantAdminCredentials,
    seedTestCoupons,
  }) => {
    test.skip(!tenantAdminCredentials, 'Tenant admin credentials required via E2E_TENANT_EMAIL/PASSWORD');

    await seedTestCoupons();

    await page.goto('/de/packages');

    const detailsButtons = page.getByRole('button', {
      name: /Details ansehen|Details anzeigen|View details/i,
    });
    await expect(detailsButtons.first()).toBeVisible();

    await detailsButtons.nth(1).click();

    const dialog = page.getByRole('dialog');
    await expect(dialog).toBeVisible();
    await expect(dialog.getByRole('heading', { name: /Standard/i })).toBeVisible();

    await dialog.getByRole('link', { name: /Jetzt bestellen|Order now|Jetzt buchen/i }).click();

    await expect(page).toHaveURL(/\/purchase-wizard\/\d+/);

    await page.getByRole('button', { name: /^Weiter$/ }).first().click();

    await expect(page.getByRole('heading', { name: /Registrieren/i })).toBeVisible();

    await page.getByRole('button', { name: /^Anmelden$/ }).first().click();

    await page.fill('input[name="identifier"]', tenantAdminCredentials.email);
    await page.fill('input[name="password"]', tenantAdminCredentials.password);
    await page.getByRole('button', { name: /^Anmelden$/ }).last().click();

    await expect(page.getByPlaceholder(/Gutscheincode/i)).toBeVisible();

    await page.getByPlaceholder(/Gutscheincode/i).fill('PERCENT10');
    await page.getByRole('button', { name: /Gutschein anwenden|Apply coupon/i }).click();

    await expect(page.getByText(/Gutschein PERCENT10 aktiviert/i)).toBeVisible();
    await expect(page.getByText(/Rabatt|Discount/i)).toBeVisible();
    await expect(page.getByText(/Total|Gesamt/i)).toBeVisible();
  });
});
