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

/**
 * Attendance Dashboard Tests
 *
 * Tests the Attendance Dashboard functionality including:
 * - Statistics cards display
 * - Team attendance table
 * - Pending regularizations table
 */

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

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

    // Verify statistics cards are visible
    await expect(page.locator('#stat-present')).toBeVisible();
    await expect(page.locator('#stat-absent')).toBeVisible();
    await expect(page.locator('#stat-late')).toBeVisible();
    await expect(page.locator('#stat-pending')).toBeVisible();
  });

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

    // Verify team attendance table is visible
    await expect(page.locator('.datatables-team-attendance')).toBeVisible();

    // Verify table headers
    await expect(page.locator('.datatables-team-attendance thead')).toContainText('Employee');
    await expect(page.locator('.datatables-team-attendance thead')).toContainText('Department');
    await expect(page.locator('.datatables-team-attendance thead')).toContainText('Status');
  });

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

    // Verify pending regularizations table is visible
    await expect(page.locator('.datatables-pending-regularizations')).toBeVisible();

    // Verify View All link exists (use getByRole to be specific)
    await expect(page.getByRole('link', { name: 'View All' })).toBeVisible();
  });

  test('should have date filter for team attendance', async ({ page }) => {
    await page.goto('/hrcore/attendance-dashboard', { waitUntil: 'networkidle' });

    // Verify date input exists
    const dateInput = page.locator('#attendance-date');
    await expect(dateInput).toBeVisible();

    // Verify refresh button exists
    const refreshButton = page.locator('button:has-text("refresh"), button:has(i.bx-refresh)').first();
    await expect(refreshButton).toBeVisible();
  });

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

    // Verify weekly summary elements exist
    await expect(page.locator('#weekly-avg')).toBeVisible();
    await expect(page.locator('#weekly-hours')).toBeVisible();
    await expect(page.locator('#team-size')).toBeVisible();
  });

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

    // Wait for chart to initialize
    await page.waitForTimeout(1000);

    // Verify chart container exists (may be hidden if no data)
    await expect(page.locator('#attendanceTrendsChart')).toBeAttached();
  });
});
