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

/**
 * Expense Types CRUD Tests
 *
 * Tests the Expense Types management functionality including:
 * - Viewing expense types list
 * - Creating a new expense type
 * - Editing an existing expense type
 * - Deleting an expense 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 name for tests
function generateUniqueName() {
  return 'TestExpense' + Date.now().toString().slice(-5);
}

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

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

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

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

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

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

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

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

  test('should display expense type form fields', async ({ page }) => {
    await page.goto('/hrcore/expense-types', { waitUntil: 'networkidle' });

    // Open offcanvas
    await page.click('button:has-text("Add Expense Type")');
    await page.waitForTimeout(500);

    // Verify form fields
    await expect(page.locator('#expenseTypeForm')).toBeVisible();
    await expect(page.locator('#name')).toBeVisible();
    await expect(page.locator('#description')).toBeVisible();
    await expect(page.locator('#status')).toBeVisible();
  });

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

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

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

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

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

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

    // Find and click the first edit button
    const editButton = page.locator('.datatables-expenseTypes tbody tr .edit-record, .datatables-expenseTypes 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('#offcanvasExpenseType')).toBeVisible();

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

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

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

    // Find the delete button
    const deleteButton = page.locator('.datatables-expenseTypes tbody tr .delete-record, .datatables-expenseTypes 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();
    }
  });
});
