import { test, expect } from '@playwright/test';
import { waitForApiCall, acceptCookieConsent } from '../helpers/utils';

test.describe('Purchase Order Management', () => {
  // Use authenticated state
  test.use({ storageState: 'tests/.auth/user.json' });

  test.beforeEach(async ({ page }) => {
    // Accept cookie consent if present
    await acceptCookieConsent(page);
  });

  test('should display action queue with pending purchase orders', async ({ page }) => {
    await page.goto('/app/dashboard');
    await acceptCookieConsent(page);

    // Wait for page to load
    await page.waitForLoadState('networkidle');

    // Look for action queue section
    const actionQueue = page.locator('[data-testid="action-queue"], :has-text("Action Queue"), :has-text("Cola de Acciones")').first();

    // Action queue should exist (even if empty)
    if (await actionQueue.isVisible().catch(() => false)) {
      await expect(actionQueue).toBeVisible();
    }
  });

  test('should approve a pending purchase order', async ({ page }) => {
    await page.goto('/app/dashboard');
    await page.waitForLoadState('networkidle');

    // Look for pending POs
    const pendingPO = page.locator('[data-testid="pending-po"], [data-testid*="purchase-order"]').first();

    if (await pendingPO.isVisible().catch(() => false)) {
      // Get the PO details before approval
      const poText = await pendingPO.textContent();

      // Find and click Approve button
      const approveButton = pendingPO.getByRole('button', { name: /approve|aprobar/i });

      if (await approveButton.isVisible().catch(() => false)) {
        await approveButton.click();

        // Should show success message
        await expect(page.locator('body')).toContainText(/approved|aprobado|success|éxito/i, {
          timeout: 5000,
        });

        // PO should be removed from queue or marked as approved
        await page.waitForTimeout(1000);
      }
    }
  });

  test('should reject a pending purchase order', async ({ page }) => {
    await page.goto('/app/dashboard');
    await page.waitForLoadState('networkidle');

    // Look for pending POs
    const pendingPO = page.locator('[data-testid="pending-po"], [data-testid*="purchase-order"]').first();

    if (await pendingPO.isVisible().catch(() => false)) {
      // Find and click Reject button
      const rejectButton = pendingPO.getByRole('button', { name: /reject|rechazar|decline/i });

      if (await rejectButton.isVisible().catch(() => false)) {
        await rejectButton.click();

        // Might show confirmation dialog
        const confirmButton = page.getByRole('button', { name: /confirm|confirmar|yes|sí/i });

        if (await confirmButton.isVisible({ timeout: 2000 }).catch(() => false)) {
          await confirmButton.click();
        }

        // Should show success message
        await expect(page.locator('body')).toContainText(/rejected|rechazado|declined/i, {
          timeout: 5000,
        });
      }
    }
  });

  test('should view purchase order details', async ({ page }) => {
    await page.goto('/app/dashboard');
    await page.waitForLoadState('networkidle');

    // Look for pending POs
    const pendingPO = page.locator('[data-testid="pending-po"], [data-testid*="purchase-order"]').first();

    if (await pendingPO.isVisible().catch(() => false)) {
      // Click on PO to view details
      await pendingPO.click();

      // Should show PO details (modal or new page)
      await page.waitForTimeout(1000);

      // Look for detail view indicators
      const detailsVisible = await page.locator('body').textContent();
      expect(detailsVisible).toMatch(/details|detalles|items|productos|supplier|proveedor/i);
    }
  });

  test('should create new purchase order from Add button', async ({ page }) => {
    await page.goto('/app/dashboard');

    // Click unified Add button
    const addButton = page.getByRole('button', { name: /^add$|^añadir$|^\+$/i }).first();

    if (await addButton.isVisible().catch(() => false)) {
      await addButton.click();

      // Wait for wizard/modal
      await page.waitForTimeout(500);

      // Look for purchase order option
      const poOption = page.getByRole('button', { name: /purchase.*order|orden.*compra/i });

      if (await poOption.isVisible().catch(() => false)) {
        await poOption.click();

        // Should show PO creation form
        await expect(page.locator('body')).toContainText(/supplier|proveedor|product|producto/i);
      }
    }
  });

  test('should filter purchase orders by status', async ({ page }) => {
    await page.goto('/app/operations/procurement');

    await page.waitForLoadState('networkidle');

    // Look for filter options
    const filterDropdown = page.locator('select, [role="combobox"]').filter({ hasText: /status|estado|filter/i }).first();

    if (await filterDropdown.isVisible().catch(() => false)) {
      // Select "Pending" filter
      await filterDropdown.click();

      const pendingOption = page.getByRole('option', { name: /pending|pendiente/i });

      if (await pendingOption.isVisible().catch(() => false)) {
        await pendingOption.click();

        // Wait for filtered results
        await page.waitForTimeout(1000);

        // Verify only pending POs are shown
        const poItems = page.locator('[data-testid*="purchase-order"]');
        const count = await poItems.count();

        // Should have at least filtered the list
        expect(count).toBeGreaterThanOrEqual(0);
      }
    }
  });

  test('should search for purchase orders', async ({ page }) => {
    await page.goto('/app/operations/procurement');

    await page.waitForLoadState('networkidle');

    // Look for search input
    const searchInput = page.getByPlaceholder(/search|buscar/i);

    if (await searchInput.isVisible().catch(() => false)) {
      // Enter search term
      await searchInput.fill('supplier');

      // Wait for search results
      await page.waitForTimeout(1000);

      // Should show filtered results
      const bodyText = await page.locator('body').textContent();
      expect(bodyText).toBeTruthy();
    }
  });
});
