/**
 * enrich.js
 * Shared logic to generate rich metadata (steps, automatable, notYet, dependencies)
 * for a test case. Used by both the editor and (optionally) the report.
 */

function enrichTestCase(t) {
  const id = t.id || '';
  const group = t.group || '';
  const isCore = t.module === 'core';
  const isPbx = t.module === 'pbx';
  const status = t.status || 'manual';

  const isAutomated = status === 'automated';
  const isPartial = status === 'partial';
  const isManual = status === 'manual';

  let steps = [];
  let automatable = [];
  let notYet = [];
  let dependencies = [];

  // === STEPS ===
  steps.push('Open admin portal or repository URL in browser context');

  if (id.includes('repository') || id.includes('documentation')) {
    steps.push('Navigate to the download / documentation portal');
    steps.push('Search or locate the relevant file/link by filename pattern');
    steps.push('Assert presence and visibility of the expected document');
  } else if (group.includes('security') || group.includes('configuration')) {
    steps.push('Log in to the admin portal (if required)');
    steps.push('Navigate to the relevant settings section');
    steps.push('Read current configuration values via UI or API');
  } else if (group.includes('calls') || group.includes('codecs')) {
    steps.push('Ensure SIP endpoints / softphones are registered');
    steps.push('Initiate or receive call using the required scenario');
    steps.push('Verify audio path, signaling, and media parameters');
  } else if (group.includes('upgrade') || group.includes('installation')) {
    steps.push('Prepare clean or previous-version VM/image');
    steps.push('Perform installation or upgrade procedure');
    steps.push('Post-install verification via portal + external tools');
  } else {
    steps.push('Execute the main verification or configuration action described in the test');
    steps.push('Capture evidence (screenshot, log, API response)');
    steps.push('Assert expected outcome');
  }

  // === AUTOMATION BREAKDOWN ===
  if (isAutomated) {
    automatable.push('Full end-to-end execution via Playwright against the admin portal UI');
    automatable.push('Navigation, form interaction, and assertions are scriptable');
  } else if (isPartial) {
    automatable.push('Portal UI configuration and verification steps (already partially automated)');
    automatable.push('Data extraction and reporting of current settings');
  }

  if (isManual || isPartial) {
    if (t.manualReason) notYet.push(t.manualReason);
    if (t.partialNotes) notYet.push(t.partialNotes);
  }

  if (!isAutomated) {
    notYet.push('Requires external SIP endpoints or physical phones for media/audio validation');
  }

  if (group.includes('virtualization') || group.includes('availability') || group.includes('performance')) {
    notYet.push('Hypervisor-level operations (vCenter, libvirt, vSphere) not reachable from browser automation');
    notYet.push('Long-running load or HA failover scenarios need dedicated lab infrastructure');
  }

  if (group.includes('calls') || group.includes('codecs')) {
    notYet.push('Real RTP/SRTP media path and audio quality verification (human ear or PESQ/MOS tools)');
    notYet.push('External SIP trunk / PSTN connectivity and DID numbers');
  }

  if (id.includes('security') && (id.includes('cve') || id.includes('vulnerab'))) {
    notYet.push('Full CVE / dependency scanning (Trivy, Grype, OWASP Dependency-Check) in CI pipeline');
  }

  // === DEPENDENCIES ===
  dependencies.push({ label: 'Admin Portal', value: 'https://pbx.local:4443 (configure via $BASE_URL)' });
  dependencies.push({ label: 'Repository URL', value: '${BASE_URL}/downloads or $REPO_URL' });

  if (isCore) dependencies.push({ label: 'Protocol', value: 'HTTPS (admin portal) + optional SSH/CLI' });
  if (isPbx) dependencies.push({ label: 'Protocol', value: 'HTTPS + SIP (UDP/TCP/TLS) + RTP/SRTP' });

  if (group.includes('calls') || group.includes('codecs') || group.includes('extensions')) {
    dependencies.push({ label: 'SIP Endpoints', value: 'At least 2 registered SIP clients (deskphone or softphone: Zoiper, MicroSIP, Yealink, etc.)' });
    dependencies.push({ label: 'Network', value: 'SIP trunk or local Asterisk/FreeSWITCH simulator for PSTN simulation' });
  }

  if (group.includes('virtualization')) {
    dependencies.push({ label: 'Hypervisor Access', value: 'vCenter, Hyper-V Manager, Proxmox, or virsh (SSH/API)' });
  }

  if (group.includes('performance') || group.includes('availability')) {
    dependencies.push({ label: 'Load Gen', value: 'SIPp or custom load generator + monitoring (Prometheus/Grafana recommended)' });
  }

  if (id.includes('siem') || id.includes('addm')) {
    dependencies.push({ label: 'External Systems', value: 'SIEM server (Splunk, ELK, QRadar) and/or ADDM/ADDM collector' });
  }

  dependencies.push({ label: 'Credentials', value: 'Admin user with sufficient privileges (portal + SSH where needed)' });
  dependencies.push({ label: 'Test Data', value: 'Clean test tenant or lab PBX instance (recommended isolated environment)' });

  // Cleanup: remove duplicates and empty
  const seen = new Set();
  notYet = notYet.filter(item => {
    const key = item.trim();
    if (!key || seen.has(key)) return false;
    seen.add(key);
    return true;
  });

  automatable = [...new Set(automatable.filter(Boolean))];
  steps = [...new Set(steps.filter(Boolean))];

  // Fallbacks
  if (automatable.length === 0) {
    automatable.push('Most UI-driven configuration and verification steps via Playwright');
  }
  if (notYet.length === 0 && !isAutomated) {
    notYet.push('No major blockers identified — can be fully automated with current tooling');
  }

  return {
    steps,
    automatable,
    notYet,
    dependencies
  };
}

module.exports = { enrichTestCase };
