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

import { login, navigateTo } from '../smoke/helpers';

test.describe('exhaustive: invoice form (billing-entity picker)', () => {
  test.beforeEach(async ({ page }) => {
    await login(page, 'super_admin');
  });

  test('new-invoice dialog has client/company toggle in billing-entity field', async ({ page }) => {
    await navigateTo(page, '/invoices');
    await page.waitForLoadState('networkidle');

    const newBtn = page.getByRole('button', { name: /new invoice|create invoice/i }).first();
    if (!(await newBtn.isVisible({ timeout: 3000 }).catch(() => false))) {
      test.skip(true, 'New invoice button not present');
      return;
    }
    await newBtn.click();

    const dialog = page.getByRole('dialog');
    await expect(dialog).toBeVisible({ timeout: 5000 });

    // The billing-entity picker should expose a client/company toggle.
    const clientOption = dialog.getByRole('button', { name: /^client$/i }).first();
    const companyOption = dialog.getByRole('button', { name: /^company$/i }).first();
    expect(
      (await clientOption.isVisible({ timeout: 3000 }).catch(() => false)) ||
        (await companyOption.isVisible({ timeout: 3000 }).catch(() => false)),
    ).toBe(true);

    const cancel = dialog.getByRole('button', { name: /cancel|close/i }).first();
    await cancel.click();
    await expect(dialog).toBeHidden({ timeout: 5000 });
  });
});
