/*
 * Priority Forge — noop memory-hub adapter
 *
 * Default backend. Zero side effects. Used until the user explicitly
 * enables sync via xmem_enable_sync. Implements BOTH MemoryHub and
 * OrchestrationHub — orchestration methods return unsupported sentinels.
 */

import {
  BlockerEdge,
  ClaimOutcome,
  ClaimResult,
  CreateSharedTaskInput,
  MemoryHub,
  MemoryHubHealth,
  OrchestrationHub,
  ReleaseResult,
  RenewResult,
  CancelResult,
  SharedTask,
  SharedTaskFilter,
  TeamActivityFilter,
  TeamMemoryEvent,
} from '../types';

export class NoopMemoryHub implements MemoryHub, OrchestrationHub {
  readonly backend = 'noop' as const;
  readonly supportsOrchestration = false;

  // ── MemoryHub ────────────────────────────────────────────────────────

  async write(_event: TeamMemoryEvent): Promise<{ accepted: boolean; remoteId?: string }> {
    return { accepted: false };
  }

  async read(_filter: TeamActivityFilter): Promise<TeamMemoryEvent[]> {
    return [];
  }

  async search(_teamId: string, _query: string, _limit?: number): Promise<TeamMemoryEvent[]> {
    return [];
  }

  async blockersFor(_teamId: string, _agentId: string): Promise<BlockerEdge[]> {
    return [];
  }

  async health(): Promise<MemoryHubHealth> {
    return {
      ok: true,
      backend: 'noop',
      detail: 'noop adapter — memory persistence disabled (enable via hub_configure)',
    };
  }

  // ── OrchestrationHub (unsupported) ────────────────────────────────────
  //
  // The noop backend cannot arbitrate claims. Methods that return values
  // emit empty / null; create() throws because there is no meaningful
  // SharedTask to return. Callers should check `supportsOrchestration`
  // before invoking, or branch on the `unsupported_backend` sentinel.

  async create(_input: CreateSharedTaskInput): Promise<SharedTask> {
    throw new Error(
      'noop backend does not support orchestration — enable a supporting backend (pgvector or dryrun) via xmem_enable_sync',
    );
  }

  async claim(
    _sharedTaskId: string,
    _agentId: string,
    _agentTeamId: string,
    _ttlMs: number,
  ): Promise<ClaimResult> {
    return { ok: false, reason: 'unsupported_backend' };
  }

  async release(
    _sharedTaskId: string,
    _agentId: string,
    _outcome: ClaimOutcome,
  ): Promise<ReleaseResult> {
    return { ok: false, reason: 'unsupported_backend' };
  }

  async renewLease(
    _sharedTaskId: string,
    _agentId: string,
    _ttlMs: number,
  ): Promise<RenewResult> {
    return { ok: false, reason: 'unsupported_backend' };
  }

  async cancel(_sharedTaskId: string, _byAgentId: string): Promise<CancelResult> {
    return { ok: false, reason: 'unsupported_backend' };
  }

  async get(_sharedTaskId: string): Promise<SharedTask | null> {
    return null;
  }

  async list(_filter: SharedTaskFilter): Promise<SharedTask[]> {
    return [];
  }

  async getMyClaims(_teamId: string, _agentId: string): Promise<SharedTask[]> {
    return [];
  }

  async searchSharedTasks(_teamId: string, _query: string, _limit?: number): Promise<SharedTask[]> {
    return [];
  }
}
