import { test, expect } from '@playwright/test';
import { login, BASE_URL } from '../../helpers/auth';
import { markPartial } from '../../helpers/manual';

test.describe('pbx-management', () => {
  test('pbx-management-check-cli_management', async ({ page }, testInfo) => {
    testInfo.annotations.push({ type: 'automationStatus', description: 'partial' });
    testInfo.annotations.push({ type: 'testType', description: 'check' });
    markPartial('CLI commands can be scripted via SSH automation (separate Node.js/shell step in CI); Playwright verifies the management web console if one exists. Full CLI coverage requires ssh2 integration alongside this test.');
    testInfo.annotations.push({ type: 'description', description: 'Verify CLI management interface is accessible and key management commands execute correctly' });

    await test.step('Navigate to admin portal — Web SSH / Console (if available)', async () => {
      await login(page);
      await page.goto(`${BASE_URL}/admin/console`);
    });

    await test.step('Verify web console loads or CLI section is accessible', async () => {
      const pageText = await page.locator('body').textContent() ?? '';
      const hasConsole = /console|terminal|ssh|cli/i.test(pageText);
      // If no web console, note it — CLI testing must be done via separate SSH step
      testInfo.annotations.push({
        type: 'note',
        description: hasConsole
          ? 'Web console found — proceed with command verification'
          : 'No web console detected — CLI testing must be done via SSH in CI pipeline',
      });
    });

    // TODO_MANUAL / TODO_SSH: Connect via SSH and verify these commands execute successfully:
    //   - `pbx-ctl status`          → shows all services running
    //   - `pbx-ctl restart sip`     → restarts SIP service without error
    //   - `pbx-cli show version`    → shows correct version string
    //   - `pbx-cli show extensions` → lists all extensions
    //   - `pbx-cli show calls`      → shows current active calls (empty if no calls)
  });

  test('pbx-management-check-gui_management', async ({ page }, testInfo) => {
    testInfo.annotations.push({ type: 'automationStatus', description: 'automated' });
    testInfo.annotations.push({ type: 'testType', description: 'check' });
    testInfo.annotations.push({ type: 'description', description: 'Verify GUI admin portal is accessible, all main sections load correctly, and management actions are functional' });

    await test.step('Login to admin portal', async () => {
      await login(page);
    });

    const sections = [
      { name: 'Dashboard', path: '/admin/dashboard' },
      { name: 'Extensions', path: '/admin/extensions' },
      { name: 'Trunks', path: '/admin/trunks' },
      { name: 'Dial Plans', path: '/admin/dialplans' },
      { name: 'Conferences', path: '/admin/conferences' },
      { name: 'Ring Groups', path: '/admin/ring-groups' },
      { name: 'IVR', path: '/admin/ivr' },
      { name: 'Voicemail', path: '/admin/voicemail' },
      { name: 'Backup', path: '/admin/backup' },
      { name: 'Security', path: '/admin/security' },
      { name: 'Logs', path: '/admin/logs' },
      { name: 'Settings', path: '/admin/settings' },
    ];

    for (const section of sections) {
      await test.step(`Verify "${section.name}" section is accessible`, async () => {
        const response = await page.goto(`${BASE_URL}${section.path}`);
        expect(
          response?.status(),
          `Section "${section.name}" returned HTTP ${response?.status()}`,
        ).toBeLessThan(500);
        await expect(page.locator('body')).not.toContainText(/500 internal server error/i);
      });
    }

    await test.step('Verify navigation menu is displayed', async () => {
      await page.goto(`${BASE_URL}/admin/dashboard`);
      await expect(page.locator('nav, .sidebar, .menu').first()).toBeVisible();
    });

    await test.step('Verify user info / logout option is visible', async () => {
      await expect(page.locator('body')).toContainText(/logout|log out|sign out|admin/i);
    });
  });
});
