/**
 * Staff Management Tests (Super Admin Only)
 */

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

test.describe('Staff Management', () => {
  test.beforeEach(async ({ page }) => {
    // Login as super admin
    await page.goto('/staff/login');
    await page.fill('input[type="text"]', 'asim');
    await page.fill('input[type="password"]', 'AsimMawidi2025!');
    await page.click('button[type="submit"]');
    await page.waitForURL('/staff/dashboard');

    // Navigate to staff management
    await page.click('nav >> text=Staff Management');
    await page.waitForURL('/staff/dashboard/staff');
  });

  test('should display staff management page', async ({ page }) => {
    await expect(page.locator('h1')).toContainText('Staff Management');
    await expect(page.locator('text=Manage staff and admin accounts')).toBeVisible();
  });

  test('should display current staff members', async ({ page }) => {
    await page.waitForTimeout(2000);

    // Should show table with staff
    await expect(page.locator('table')).toBeVisible();
    await expect(page.locator('th:has-text("Staff Member")')).toBeVisible();
    await expect(page.locator('th:has-text("Username")')).toBeVisible();
    await expect(page.locator('th:has-text("Role")')).toBeVisible();
    await expect(page.locator('th:has-text("Status")')).toBeVisible();

    // Should show at least the super admin (Asim)
    await expect(page.locator('td:has-text("asim")')).toBeVisible();
  });

  test('should have add staff member button', async ({ page }) => {
    const addButton = page.locator('button:has-text("Add Staff Member")');
    await expect(addButton).toBeVisible();
  });

  test('should open create staff modal', async ({ page }) => {
    await page.waitForTimeout(1000);

    // Click add button
    await page.click('button:has-text("Add Staff Member")');

    // Modal should appear
    await expect(page.locator('text=Add Staff Member')).toBeVisible();
    await expect(page.locator('input[name="name"]')).toBeVisible();
    await expect(page.locator('input[name="email"]')).toBeVisible();
    await expect(page.locator('input[name="username"]')).toBeVisible();
    await expect(page.locator('input[name="password"]')).toBeVisible();
    await expect(page.locator('select[name="role"]')).toBeVisible();

    // Check role options
    const roleSelect = page.locator('select[name="role"]');
    await expect(roleSelect.locator('option:has-text("Staff")')).toBeVisible();
    await expect(roleSelect.locator('option:has-text("Admin")')).toBeVisible();
    await expect(roleSelect.locator('option:has-text("Super Admin")')).toBeVisible();

    // Close modal
    await page.click('button:has-text("Cancel")');
    await expect(page.locator('input[name="name"]')).not.toBeVisible();
  });

  test('should create new staff member', async ({ page }) => {
    await page.waitForTimeout(1000);

    // Open create modal
    await page.click('button:has-text("Add Staff Member")');

    // Fill form
    const timestamp = Date.now();
    await page.fill('input[name="name"]', 'Test Staff');
    await page.fill('input[name="email"]', `teststaff${timestamp}@mawidi.com`);
    await page.fill('input[name="username"]', `teststaff${timestamp}`);
    await page.fill('input[name="password"]', 'TestPassword123!');
    await page.selectOption('select[name="role"]', 'STAFF');

    // Submit form
    await page.click('button[type="submit"]:has-text("Create")');

    // Wait for modal to close and table to refresh
    await page.waitForTimeout(2000);

    // Should show new staff in table
    await expect(page.locator(`td:has-text("teststaff${timestamp}")`)).toBeVisible();
  });

  test('should display staff status badges', async ({ page }) => {
    await page.waitForTimeout(2000);

    // Check for active/inactive badges
    const statusBadges = page.locator('span:has-text(/Active|Inactive/)');
    const count = await statusBadges.count();

    expect(count).toBeGreaterThan(0);
  });

  test('should have activate/deactivate buttons for other staff', async ({ page }) => {
    await page.waitForTimeout(2000);

    // Current user (asim) should NOT have deactivate button on own row
    const asimRow = page.locator('tr:has-text("asim")');
    const asimButtons = asimRow.locator('button:has-text(/Activate|Deactivate/)');
    expect(await asimButtons.count()).toBe(0);

    // Other staff members should have buttons
    // (This test will pass if there are other staff members)
  });

  test('should display role badges correctly', async ({ page }) => {
    await page.waitForTimeout(2000);

    // Check role badges exist
    const roleBadges = page.locator('span.rounded-full:has-text(/STAFF|ADMIN|SUPER/)');
    const count = await roleBadges.count();

    expect(count).toBeGreaterThan(0);
  });

  test('should show last login information', async ({ page }) => {
    await page.waitForTimeout(2000);

    // Check table has Last Login column
    await expect(page.locator('th:has-text("Last Login")')).toBeVisible();

    // Should show either a date or "Never"
    const loginCells = page.locator('td').filter({ hasText: /\d{1,2}\/\d{1,2}\/\d{4}|Never/ });
    expect(await loginCells.count()).toBeGreaterThan(0);
  });
});
