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

test.describe('core-interoperability', () => {
  test.beforeEach(async ({ page }) => {
    await login(page);
  });

  test('core-interoperability-check-addm', async ({ page }, testInfo) => {
    testInfo.annotations.push({ type: 'automationStatus', description: 'automated' });
    testInfo.annotations.push({ type: 'testType', description: 'check' });
    testInfo.annotations.push({ type: 'description', description: 'Verify ADDM (Automatic Discovery and Dependency Mapping) integration is configured and reporting data correctly' });

    await test.step('Navigate to Integrations → ADDM', async () => {
      await page.goto(`${BASE_URL}/admin/integrations/addm`);
    });

    await test.step('Verify ADDM integration page loads without errors', async () => {
      await expect(page.locator('body')).not.toContainText(/500|not found/i);
    });

    await test.step('Verify ADDM connection status is shown', async () => {
      const status = page.locator('.connection-status, .addm-status, [data-field="addm-status"]').first();
      await expect(status).toBeVisible();
    });

    await test.step('Verify ADDM last discovery timestamp is recent', async () => {
      const tsEl = page.locator('[data-field="last-discovery"],[class*="timestamp"],[class*="last-sync"]').first();
      if (await tsEl.count() > 0) {
        const tsText = await tsEl.textContent() ?? '';
        expect(tsText.trim()).not.toBe('');
      }
    });

    await test.step('Verify no ADDM errors are reported', async () => {
      await expect(page.locator('.alert-danger, .error-badge').filter({ hasText: /addm/i })).toHaveCount(0);
    });
  });

  test('core-interoperability-check-siem', async ({ page }, testInfo) => {
    testInfo.annotations.push({ type: 'automationStatus', description: 'automated' });
    testInfo.annotations.push({ type: 'testType', description: 'check' });
    testInfo.annotations.push({ type: 'description', description: 'Verify SIEM (Security Information and Event Management) integration is configured and forwarding security events' });

    await test.step('Navigate to Integrations → SIEM', async () => {
      await page.goto(`${BASE_URL}/admin/integrations/siem`);
    });

    await test.step('Verify SIEM integration page loads without errors', async () => {
      await expect(page.locator('body')).not.toContainText(/500|not found/i);
    });

    await test.step('Verify SIEM destination (syslog/SIEM server) is configured', async () => {
      const destField = page.locator('[name*="siem"],[name*="syslog"],[id*="siem"],[id*="syslog"]').first();
      if (await destField.count() > 0) {
        const value = await destField.inputValue();
        expect(value.trim()).not.toBe('');
      }
    });

    await test.step('Verify SIEM event forwarding is enabled', async () => {
      const enableToggle = page.locator('[name*="siem_enabled"],[id*="siem-enable"],[data-field="siem-enabled"]').first();
      if (await enableToggle.count() > 0) {
        await expect(enableToggle).toBeChecked();
      }
    });

    await test.step('Verify SIEM connection status shows no errors', async () => {
      await expect(page.locator('.siem-status, .connection-status').filter({ hasText: /error|fail/i })).toHaveCount(0);
    });
  });
});
