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

test.describe('Grievance Document MIME Type Restrictions', () => {
  test('grievance document upload enforces MIME type restrictions', async ({ page }) => {
    await page.goto('/hr-manager/grievances/list');

    await page.waitForSelector('table tbody', { timeout: 20000 });

    const rowCount = await page.locator('table tbody tr:not([aria-hidden="true"])').count();

    if (rowCount === 0) {
      test.skip(true, 'No grievances available for testing');
    }

    const viewButton = page
      .locator('table tbody tr:not([aria-hidden="true"])')
      .first()
      .locator('button[aria-label="View"]');
    await viewButton.click();

    await page.waitForURL('**/grievances/view/**', { timeout: 10000 });

    const documentsTab = page.getByRole('tab', { name: /documents/i });
    if (await documentsTab.isVisible()) {
      await documentsTab.click();

      await page.waitForTimeout(2000);

      const uploadButton = page.locator('button:has-text("Upload")');
      if (await uploadButton.isVisible()) {
        test.skip(false, 'Document upload test requires upload button to be visible');
      }
    } else {
      test.skip(true, 'Documents tab not available');
    }
  });
});
