/**
 * Registry of RMM Overshell scripts.
 *
 * Adding a new script:
 *   1. Create lib/services/rmm/scripts/<id>.ts exporting an RmmScript.
 *   2. Import + add to SCRIPTS below.
 *   3. Bump the script's `version` field if you change body or output shape.
 *   4. Run npm test — registry tests verify ids are unique + ids match
 *      module exports.
 *
 * The DB never stores executable PowerShell. The runtime gates execution to
 * the ids in this registry only.
 */

import type { RmmScript } from './types';
import { getServices } from './get-services';
import { getInstalledSoftware } from './get-installed-software';
import { getEventLogRecent } from './get-event-log-recent';
import { getAdHealth } from './get-ad-health';
import { getDhcpScopes } from './get-dhcp-scopes';
import { getDnsZones } from './get-dns-zones';
import { getNetworkDiscovery } from './get-network-discovery';
import { logliftEventLogs } from './loglift-eventlogs';

const _all: RmmScript[] = [
  getServices,
  getInstalledSoftware,
  getEventLogRecent,
  getAdHealth,
  getDhcpScopes,
  getDnsZones,
  getNetworkDiscovery,
  logliftEventLogs,
];

// Build the lookup map and verify ids are unique at module-load time.
export const SCRIPTS: Readonly<Record<string, RmmScript>> = (() => {
  const map: Record<string, RmmScript> = {};
  for (const s of _all) {
    if (map[s.id]) {
      throw new Error(`RMM script registry: duplicate id "${s.id}"`);
    }
    map[s.id] = s;
  }
  return Object.freeze(map);
})();

export function listScripts(): RmmScript[] {
  return Object.values(SCRIPTS).sort((a, b) => a.name.localeCompare(b.name));
}

export function getScript(id: string): RmmScript | null {
  return SCRIPTS[id] ?? null;
}

export type { RmmScript } from './types';
export { parseJsonOutput } from './types';
