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

/**
 * Leave Requests List Tests
 *
 * Tests the Leave Requests list functionality including:
 * - Viewing leave requests
 * - Filtering by employee, date, leave type, status
 * - Opening add leave offcanvas
 */

// Helper function to login as admin
async function loginAsAdmin(page) {
  await page.goto('/auth/login', { waitUntil: 'networkidle' });
  await page.locator('#email').fill('admin@demo.com');
  await page.locator('#password').fill('password123');
  await page.locator('button[type="submit"]').click();
  await page.waitForURL('**/dashboard', { timeout: 15000 });
}

test.describe('Leave Requests List', () => {
  test.beforeEach(async ({ page }) => {
    await loginAsAdmin(page);
  });

  test('should display leave requests list page', async ({ page }) => {
    // Navigate to leave requests page
    await page.goto('/hrcore/leaves', { waitUntil: 'networkidle' });

    // Verify page elements
    await expect(page.locator('.card').first()).toBeVisible();
    await expect(page.locator('#leaveRequestsTable')).toBeVisible();
  });

  test('should display filter options', async ({ page }) => {
    await page.goto('/hrcore/leaves', { waitUntil: 'networkidle' });

    // Verify filter elements
    await expect(page.locator('#employeeFilter')).toBeVisible();
    await expect(page.locator('#dateFilter')).toBeVisible();
    await expect(page.locator('#leaveTypeFilter')).toBeVisible();
    await expect(page.locator('#statusFilter')).toBeVisible();
  });

  test('should display table columns correctly', async ({ page }) => {
    await page.goto('/hrcore/leaves', { waitUntil: 'networkidle' });

    // Verify table headers
    const tableHeader = page.locator('#leaveRequestsTable thead');
    await expect(tableHeader).toContainText('Employee');
    await expect(tableHeader).toContainText('Leave Type');
    await expect(tableHeader).toContainText('Status');
  });

  test('should filter by status', async ({ page }) => {
    await page.goto('/hrcore/leaves', { waitUntil: 'networkidle' });

    // Select pending status
    await page.selectOption('#statusFilter', 'pending');
    await page.waitForTimeout(1000);

    // Table should still be visible
    await expect(page.locator('#leaveRequestsTable')).toBeVisible();
  });

  test('should filter by leave type', async ({ page }) => {
    await page.goto('/hrcore/leaves', { waitUntil: 'networkidle' });

    // Check if there are leave type options
    const leaveTypeOptions = page.locator('#leaveTypeFilter option');
    if (await leaveTypeOptions.count() > 1) {
      // Select the first leave type
      await page.locator('#leaveTypeFilter').selectOption({ index: 1 });
      await page.waitForTimeout(1000);
    }

    // Table should still be visible
    await expect(page.locator('#leaveRequestsTable')).toBeVisible();
  });

  test('should open add leave request offcanvas', async ({ page }) => {
    await page.goto('/hrcore/leaves', { waitUntil: 'networkidle' });

    // Click Add Leave Request button
    await page.click('button:has-text("Add Leave Request")');
    await page.waitForTimeout(500);

    // Verify offcanvas is visible
    await expect(page.locator('#offcanvasAddLeave')).toBeVisible();

    // Verify form fields
    await expect(page.locator('#leaveRequestForm')).toBeVisible();
    await expect(page.locator('#leave_type_id')).toBeVisible();
    await expect(page.locator('#user_notes')).toBeVisible();
  });

  test('should close leave offcanvas on cancel', async ({ page }) => {
    await page.goto('/hrcore/leaves', { waitUntil: 'networkidle' });

    // Open offcanvas
    await page.click('button:has-text("Add Leave Request")');
    await page.waitForTimeout(500);
    await expect(page.locator('#offcanvasAddLeave')).toBeVisible();

    // Click cancel button
    await page.locator('#offcanvasAddLeave button[data-bs-dismiss="offcanvas"]').first().click();
    await page.waitForTimeout(500);

    // Verify offcanvas is hidden
    await expect(page.locator('#offcanvasAddLeave')).toBeHidden();
  });

  test('should display leave duration options', async ({ page }) => {
    await page.goto('/hrcore/leaves', { waitUntil: 'networkidle' });

    // Open add leave offcanvas
    await page.click('button:has-text("Add Leave Request")');
    await page.waitForTimeout(500);

    // Verify leave duration radio buttons
    await expect(page.locator('#full_day')).toBeVisible();
    await expect(page.locator('#half_day')).toBeVisible();
  });

  test('should toggle between full day and half day options', async ({ page }) => {
    await page.goto('/hrcore/leaves', { waitUntil: 'networkidle' });

    // Open add leave offcanvas
    await page.click('button:has-text("Add Leave Request")');
    await page.waitForTimeout(500);

    // Verify full day dates are visible by default
    await expect(page.locator('#full_day_dates')).toBeVisible();
    await expect(page.locator('#half_day_options')).toBeHidden();

    // Click half day option
    await page.locator('#half_day').click();
    await page.waitForTimeout(300);

    // Verify half day options are visible
    await expect(page.locator('#half_day_options')).toBeVisible();

    // Close offcanvas
    await page.locator('#offcanvasAddLeave button[data-bs-dismiss="offcanvas"]').first().click();
  });
});
