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

test.describe('Package Flow in Admin PWA', () => {
  test('Create event with package and verify limits', async ({ page }) => {
    // Assume logged in as tenant admin, navigate to events page
    await page.goto('/event-admin/events');

    // Click create event button
    await page.click('[data-testid="create-event"]');
    await expect(page).toHaveURL(/\/event-admin\/events\/create/);

    // Fill form
    await page.fill('[name="name"]', 'Test Package Event');
    await page.fill('[name="slug"]', 'test-package-event');
    await page.fill('[name="date"]', '2025-10-01');

    // Select package from dropdown
    await page.selectOption('[name="package_id"]', '1'); // Assume ID 1 is Starter package
    await expect(page.locator('[name="package_id"]')).toHaveValue('1');

    // Submit
    await page.click('[type="submit"]');
    await expect(page).toHaveURL(/\/event-admin\/events/);

    // Verify event created and package assigned
    await expect(page.locator('text=Test Package Event')).toBeVisible();
    await expect(page.locator('text=Starter')).toBeVisible(); // Package name in table

    // Check dashboard limits
    await page.goto('/event-admin/events');
    await expect(page.locator('text=Remaining Photos')).toContainText('300'); // Starter limit

    // Try to create another event to test reseller limit if applicable
    // (Skip for endcustomer; assume tenant has reseller package with limit 1)
    await page.goto('/event-admin/events');
    await page.click('[data-testid="create-event"]');
    await page.fill('[name="name"]', 'Second Event');
    await page.fill('[name="slug"]', 'second-event');
    await page.fill('[name="date"]', '2025-10-02');
    await page.selectOption('[name="package_id"]', '1');
    await page.click('[type="submit"]');

    // If limit reached, expect error
    await expect(page.locator('text=No available package')).toBeVisible();
  });

  test('Upload blocked when package limit reached in Guest PWA', async ({ page }) => {
    // Assume event with package limit 0 created
    await page.goto('/e/test-limited-event'); // Slug of event with max_photos = 0

    // Navigate to upload
    await page.click('text=Upload');
    await expect(page).toHaveURL(/\/upload/);

    // Expect upload disabled and error message
    await expect(page.locator('button:disabled')).toBeVisible(); // Upload button disabled
    await expect(page.locator('text=Upload-Limit erreicht')).toBeVisible();
  });
});
