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

/**
 * Attendance Regularization Tests
 *
 * Tests the Attendance Regularization functionality including:
 * - Viewing regularization requests
 * - Creating regularization request
 * - Filtering by status/type
 * - Statistics cards
 */

// 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('Attendance Regularization', () => {
  test.beforeEach(async ({ page }) => {
    await loginAsAdmin(page);
  });

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

    // Verify page elements
    await expect(page.locator('.card').first()).toBeVisible();
    await expect(page.locator('.datatables-regularization')).toBeVisible();
  });

  test('should display statistics cards', async ({ page }) => {
    await page.goto('/hrcore/attendance-regularization', { waitUntil: 'networkidle' });

    // Verify statistics cards
    await expect(page.locator('#totalCount')).toBeVisible();
    await expect(page.locator('#pendingCount')).toBeVisible();
    await expect(page.locator('#approvedToday')).toBeVisible();
    await expect(page.locator('#rejectedToday')).toBeVisible();
  });

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

    // Verify filter elements
    await expect(page.locator('#date-from')).toBeVisible();
    await expect(page.locator('#date-to')).toBeVisible();
    await expect(page.locator('#status-filter')).toBeVisible();
    await expect(page.locator('#type-filter')).toBeVisible();
  });

  test('should open create regularization offcanvas', async ({ page }) => {
    await page.goto('/hrcore/attendance-regularization', { waitUntil: 'networkidle' });

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

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

    // Verify form fields
    await expect(page.locator('#regularizationForm #date, #regularizationForm [name="date"]').first()).toBeVisible();
    await expect(page.locator('#regularizationForm #type')).toBeVisible();
    await expect(page.locator('#regularizationForm #reason')).toBeVisible();
  });

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

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

    // Click cancel button (use first to avoid strict mode violation)
    await page.locator('#regularizationOffcanvas button[data-bs-dismiss="offcanvas"]').first().click();
    await page.waitForTimeout(500);

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

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

    // Select pending status
    await page.selectOption('#status-filter', 'pending');

    // Click apply filters
    await page.click('button:has-text("Apply Filters")');
    await page.waitForTimeout(1000);

    // Table should still be visible
    await expect(page.locator('.datatables-regularization')).toBeVisible();
  });

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

    // Select type
    await page.selectOption('#type-filter', 'missing_checkin');

    // Click apply filters
    await page.click('button:has-text("Apply Filters")');
    await page.waitForTimeout(1000);

    // Table should still be visible
    await expect(page.locator('.datatables-regularization')).toBeVisible();
  });

  test('should reset filters', async ({ page }) => {
    await page.goto('/hrcore/attendance-regularization', { waitUntil: 'networkidle' });

    // Set a filter
    await page.selectOption('#status-filter', 'pending');

    // Click reset filters
    await page.click('button:has-text("Reset Filters")');
    await page.waitForTimeout(500);

    // Verify filter is reset
    const statusValue = await page.locator('#status-filter').inputValue();
    expect(statusValue).toBe('');
  });

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

    // Verify table headers
    const tableHeader = page.locator('.datatables-regularization thead');
    await expect(tableHeader).toContainText('Employee');
    await expect(tableHeader).toContainText('Department');
    await expect(tableHeader).toContainText('Type');
    await expect(tableHeader).toContainText('Status');
  });
});
