/**
 * SwitchboardDispatchMode — WP-08.
 *
 * off     → legacy TaskDispatcher / SessionManager path only
 * shadow  → Switchboard may record / claim; no production Active cutover
 * active  → Switchboard is the sole Host send path (requires Gate B token)
 *
 * Production Active on 18766 is forbidden until Gate B approve_active.
 */

export type SwitchboardDispatchMode = "off" | "shadow" | "active";

/** Explicit Gate B approve_active token — never invent; caller must supply. */
export interface ActiveAuthorization {
  approved: true;
  /** Formal ADMIN decision id / report ref / chat authorization id. */
  decisionRef: string;
  approvedAt: string;
  approvedBy: string;
}

export class SwitchboardModeError extends Error {
  constructor(message: string) {
    super(message);
    this.name = "SwitchboardModeError";
  }
}

export function assertActiveAuthorized(
  mode: SwitchboardDispatchMode,
  auth: ActiveAuthorization | null | undefined,
): void {
  if (mode !== "active") return;
  if (!auth?.approved || !String(auth.decisionRef ?? "").trim()) {
    throw new SwitchboardModeError(
      "active mode requires Gate B approve_active (ActiveAuthorization.decisionRef)",
    );
  }
}

export function isHostSendAllowed(
  mode: SwitchboardDispatchMode,
  auth: ActiveAuthorization | null | undefined,
): boolean {
  if (mode === "off") return false;
  if (mode === "shadow") return true; // InMemory / test hosts only; not 18766 Active
  assertActiveAuthorized(mode, auth);
  return true;
}
