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

/**
 * Audit Logs Tests
 *
 * Tests the Audit Logs functionality including:
 * - Viewing audit logs list
 * - Audit log table columns
 * - View audit log details
 */

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

  test('should display audit logs page', async ({ page }) => {
    // Navigate to audit logs page
    await page.goto('/auditLogs', { waitUntil: 'networkidle' });

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

  test('should display audit log table columns', async ({ page }) => {
    await page.goto('/auditLogs', { waitUntil: 'networkidle' });

    // Verify table headers
    const tableHeader = page.locator('#datatable thead');
    await expect(tableHeader).toContainText('User');
    await expect(tableHeader).toContainText('Event');
    await expect(tableHeader).toContainText('Ip');
    await expect(tableHeader).toContainText('Model');
    await expect(tableHeader).toContainText('Created At');
    await expect(tableHeader).toContainText('Actions');
  });

  test('should have view button for audit logs', async ({ page }) => {
    await page.goto('/auditLogs', { waitUntil: 'networkidle' });

    // Wait for table to load
    await page.waitForTimeout(1000);

    // Check if view button exists
    const viewButton = page.locator('#datatable tbody tr a.btn-icon .bx-show').first();
    if (await viewButton.count() > 0) {
      await expect(viewButton).toBeVisible();
    }
  });

  test('should navigate to audit log details page', async ({ page }) => {
    await page.goto('/auditLogs', { waitUntil: 'networkidle' });

    // Wait for table to load
    await page.waitForTimeout(1000);

    // Find and click the view button if there are audit logs
    const viewButton = page.locator('#datatable tbody tr a.btn-icon').first();
    if (await viewButton.count() > 0) {
      await viewButton.click();
      await page.waitForURL('**/auditLogs/show/**', { timeout: 10000 });

      // Verify we're on the details page
      await expect(page.locator('.card').first()).toBeVisible();
    }
  });

  test('should display audit log details fields', async ({ page }) => {
    await page.goto('/auditLogs', { waitUntil: 'networkidle' });

    // Wait for table to load
    await page.waitForTimeout(1000);

    // Navigate to first audit log details if exists
    const viewButton = page.locator('#datatable tbody tr a.btn-icon').first();
    if (await viewButton.count() > 0) {
      await viewButton.click();
      await page.waitForURL('**/auditLogs/show/**', { timeout: 10000 });

      // Verify details page elements - use card header for reliable check
      await expect(page.getByRole('heading', { name: 'Audit Log Details' })).toBeVisible();

      // Check for expected fields in page content
      await expect(page.locator('.card-body').first()).toContainText('User');
      await expect(page.locator('.card-body').first()).toContainText('Event');
    }
  });
});
