import { test, expect } from '@playwright/test';
import {
  loginAsAdmin,
  navigateToResources,
  navigateToResourceCapacity,
  fillSelect2ById,
  setFlatpickrDate,
  waitForDataTable,
  waitForPageStable,
  getTodayDate,
  getFutureDate
} from './helpers/pmcore';

/**
 * PM Core Resource Management Tests
 *
 * Tests the Resource Allocation management functionality including:
 * - Viewing resource allocations list
 * - Creating a new resource allocation
 * - Viewing resource capacity page
 */

test.describe('Resource Management', () => {
  test.beforeEach(async ({ page }) => {
    await loginAsAdmin(page);
  });

  test('should display resource list', async ({ page }) => {
    await test.step('Navigate to resources page', async () => {
      await navigateToResources(page);
    });

    await test.step('Verify resource page loads', async () => {
      await waitForPageStable(page);
      await expect(page.locator('.card').first()).toBeVisible();
    });

    await test.step('Verify statistics cards are visible', async () => {
      // Check for stat cards
      const totalResourcesCard = page.locator('text=Total Resources').first();
      await expect(totalResourcesCard).toBeVisible({ timeout: 10000 });
    });

    await test.step('Verify add resource allocation button exists', async () => {
      const addButton = page.locator('a:has-text("Allocate Resource"), a[href*="/projects/resources/create"]').first();
      await expect(addButton).toBeVisible();
    });
  });

  test('should navigate to resource allocation create', async ({ page }) => {
    await test.step('Navigate to resources list', async () => {
      await navigateToResources(page);
      await waitForPageStable(page);
    });

    await test.step('Click allocate resource button', async () => {
      const addButton = page.locator('a:has-text("Allocate Resource"), a[href*="/projects/resources/create"]').first();
      await addButton.click();
    });

    await test.step('Verify create page loads', async () => {
      await page.waitForURL('**/projects/resources/create', { timeout: 10000 });
      await waitForPageStable(page);

      const pageTitle = page.locator('h4, h5').filter({ hasText: /Allocate Resource/i }).first();
      await expect(pageTitle).toBeVisible();
    });
  });

  test('should display create allocation form elements', async ({ page }) => {
    await test.step('Navigate to create page', async () => {
      await page.goto('/projects/resources/create', { waitUntil: 'networkidle' });
      await waitForPageStable(page);
    });

    await test.step('Verify form elements exist', async () => {
      // Check for user select (Select2)
      const userSelect = page.locator('#user_id').first();
      await expect(userSelect).toBeVisible({ timeout: 5000 });

      // Check for project select (Select2)
      const projectSelect = page.locator('#project_id').first();
      await expect(projectSelect).toBeVisible();

      // Check for date fields
      const startDateField = page.locator('#start_date').first();
      await expect(startDateField).toBeVisible();

      const endDateField = page.locator('#end_date').first();
      await expect(endDateField).toBeVisible();

      // Check for allocation percentage
      const allocationField = page.locator('#allocation_percentage').first();
      await expect(allocationField).toBeVisible();

      // Check for hours per day
      const hoursField = page.locator('#hours_per_day').first();
      await expect(hoursField).toBeVisible();

      // Check for submit button
      const submitButton = page.locator('button[type="submit"]:has-text("Allocate Resource")').first();
      await expect(submitButton).toBeVisible();

      // Check for cancel button
      const cancelButton = page.locator('a:has-text("Cancel")').first();
      await expect(cancelButton).toBeVisible();
    });
  });

  test('should navigate to capacity page', async ({ page }) => {
    await test.step('Navigate to resources list', async () => {
      await navigateToResources(page);
      await waitForPageStable(page);
    });

    await test.step('Click capacity planning button', async () => {
      const capacityButton = page.locator('a:has-text("Capacity Planning"), a[href*="/capacity"]').first();
      await capacityButton.click();
    });

    await test.step('Verify capacity page loads', async () => {
      await page.waitForURL('**/projects/resources/capacity', { timeout: 10000 });
      await waitForPageStable(page);

      const mainContent = page.locator('.card, .content-wrapper, main').first();
      await expect(mainContent).toBeVisible();
    });
  });

  test('should cancel creation and return to list', async ({ page }) => {
    await test.step('Navigate to create page', async () => {
      await page.goto('/projects/resources/create', { waitUntil: 'networkidle' });
      await waitForPageStable(page);
    });

    await test.step('Click cancel and verify return to list', async () => {
      const cancelButton = page.locator('a:has-text("Cancel")').first();
      await cancelButton.click();

      await page.waitForURL('**/projects/resources', { timeout: 10000 });
      await waitForPageStable(page);

      // Verify we're back on the list page
      const pageTitle = page.locator('h4, h5').filter({ hasText: /Resource Management|Resources/i }).first();
      await expect(pageTitle).toBeVisible();
    });
  });

  test('should show form with allocation type options', async ({ page }) => {
    await test.step('Navigate to create page', async () => {
      await page.goto('/projects/resources/create', { waitUntil: 'networkidle' });
      await waitForPageStable(page);
    });

    await test.step('Verify allocation type select options', async () => {
      const allocationTypeSelect = page.locator('#allocation_type').first();
      await expect(allocationTypeSelect).toBeVisible();

      // Check options exist
      const projectOption = allocationTypeSelect.locator('option[value="project"]');
      await expect(projectOption).toBeAttached();

      const phaseOption = allocationTypeSelect.locator('option[value="phase"]');
      await expect(phaseOption).toBeAttached();

      const taskOption = allocationTypeSelect.locator('option[value="task"]');
      await expect(taskOption).toBeAttached();
    });
  });

  test('should have billable and confirmed checkboxes', async ({ page }) => {
    await test.step('Navigate to create page', async () => {
      await page.goto('/projects/resources/create', { waitUntil: 'networkidle' });
      await waitForPageStable(page);
    });

    await test.step('Verify checkbox options', async () => {
      const billableCheckbox = page.locator('#is_billable').first();
      await expect(billableCheckbox).toBeVisible();

      // Billable should be checked by default
      await expect(billableCheckbox).toBeChecked();

      const confirmedCheckbox = page.locator('#is_confirmed').first();
      await expect(confirmedCheckbox).toBeVisible();

      // Confirmed should not be checked by default
      await expect(confirmedCheckbox).not.toBeChecked();
    });
  });
});
