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

/**
 * Leave Types CRUD Tests
 *
 * Tests the Leave Types management functionality including:
 * - Viewing leave types list
 * - Creating a new leave type
 * - Editing an existing leave type
 * - Deleting a leave type
 */

// 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 });
}

// Generate unique code for tests
function generateUniqueCode() {
  return 'LT' + Date.now().toString().slice(-5);
}

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

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

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

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

    // Verify table headers
    const tableHeader = page.locator('.datatables-leaveTypes thead');
    await expect(tableHeader).toContainText('Name');
    await expect(tableHeader).toContainText('Code');
    await expect(tableHeader).toContainText('Status');
  });

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

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

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

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

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

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

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

  test('should open edit leave type offcanvas', async ({ page }) => {
    await page.goto('/hrcore/leave-types', { waitUntil: 'networkidle' });

    // Wait for DataTable to load
    await page.waitForSelector('.datatables-leaveTypes tbody tr', { timeout: 10000 });

    // Find and click the first edit button
    const editButton = page.locator('.datatables-leaveTypes tbody tr .edit-record, .datatables-leaveTypes tbody tr button[onclick*="edit"]').first();

    if (await editButton.count() > 0) {
      await editButton.click();
      await page.waitForTimeout(500);

      // Verify offcanvas opens
      await expect(page.locator('#offcanvasAddOrUpdateLeaveType')).toBeVisible();

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

  test('should delete leave type with confirmation', async ({ page }) => {
    await page.goto('/hrcore/leave-types', { waitUntil: 'networkidle' });

    // Wait for DataTable to load
    await page.waitForSelector('.datatables-leaveTypes tbody tr', { timeout: 10000 });

    // Find the delete button
    const deleteButton = page.locator('.datatables-leaveTypes tbody tr .delete-record, .datatables-leaveTypes tbody tr button[onclick*="delete"]').first();

    if (await deleteButton.count() > 0) {
      await deleteButton.click();

      // Wait for SweetAlert2 confirmation dialog
      await page.waitForSelector('.swal2-popup', { state: 'visible', timeout: 5000 });

      // Verify SweetAlert2 dialog is shown
      await expect(page.locator('.swal2-popup')).toBeVisible();

      // Click Cancel to dismiss without deleting
      await page.locator('.swal2-cancel').click();
      await page.waitForTimeout(500);

      // Verify SweetAlert2 is closed
      await expect(page.locator('.swal2-popup')).toBeHidden();
    }
  });
});
