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

test.describe('Statutory Deductions Edit', () => {
  test('can edit statutory deductions fields', async ({ page }) => {
    await page.goto('/hr-manager/employees/list');

    await page.waitForSelector('table tbody tr:not([aria-hidden="true"])', { timeout: 20000 });

    const rowCount = await page.locator('table tbody tr:not([aria-hidden="true"])').count();
    if (rowCount === 0) {
      test.skip(true, 'No employees 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('**/employees/view/**', { timeout: 10000 });

    await page.getByRole('tab', { name: /financial & statutory/i }).click();

    await expect(page.getByText('Statutory Deductions')).toBeVisible({ timeout: 10000 });

    const statutoryCard = page.locator('.ant-pro-card').filter({ hasText: 'Statutory Deductions' });
    const editButton = statutoryCard.locator('button:has-text("Edit")');
    await editButton.click();

    await expect(page.getByText('Edit Statutory Deductions')).toBeVisible({ timeout: 5000 });

    await page.getByLabel('Tier 2 Provider').fill('Test Tier 2 Provider');
    await page.getByLabel('Tier 2 Number').fill('T2-12345');
    await page.getByLabel('Tier 3 Provider').fill('Test Tier 3 Provider');
    await page.getByLabel('Tier 3 Number').fill('T3-67890');
    await page.getByLabel('Provident Fund Number').fill('PF-11111');

    await page.getByRole('button', { name: /ok/i }).click();

    await expect(
      page.getByText(/successfully updated statutory deductions/i),
    ).toBeVisible({ timeout: 10000 });

    await expect(page.getByText('Test Tier 2 Provider')).toBeVisible();
    await expect(page.getByText('T2-12345')).toBeVisible();
  });
});
