import { compactSubmitSuccessResponseForTest } from "./src/code-queue";

type JsonRecord = Record<string, unknown>;

function assertCondition(condition: unknown, message: string, detail: unknown = {}): void {
  if (!condition) throw new Error(`${message}: ${JSON.stringify(detail)}`);
}

function asRecord(value: unknown): JsonRecord {
  assertCondition(typeof value === "object" && value !== null && !Array.isArray(value), "expected JSON object", { value });
  return value as JsonRecord;
}

function asArray(value: unknown): unknown[] {
  assertCondition(Array.isArray(value), "expected JSON array", { value });
  return value as unknown[];
}

function assertNoItemsArrayWhenUnavailable(value: JsonRecord, label: string): void {
  assertCondition(value.idsUnavailable === true, `${label} should mark non-enumerated ids unavailable`, value);
  assertCondition(!Object.prototype.hasOwnProperty.call(value, "items"), `${label} must not emit items=[] when count is nonzero but ids are unavailable`, value);
  assertCondition(value.itemsOmitted === true, `${label} should explicitly mark items omitted`, value);
  assertCondition(String(value.itemsMeaning || "") === "not-enumerated-in-default-submit-output", `${label} should explain empty-list semantics`, value);
  assertCondition(String(value.rawCommand || "").includes("microservice proxy code-queue /api/tasks/overview"), `${label} should provide raw drill-down`, value);
}

function task(id: string, status: string, queueId = "commander-efficiency"): JsonRecord {
  return {
    id,
    queueId,
    status,
    prompt: `Focused submit summary contract for ${id}`,
    displayPrompt: `Focused submit summary contract for ${id}`,
    providerId: "D601",
    model: "gpt-5.5",
    currentAttempt: status === "queued" ? 0 : 1,
    maxAttempts: 99,
    createdAt: "2026-05-23T00:00:00.000Z",
    updatedAt: "2026-05-23T00:00:00.000Z",
  };
}

function manyIds(prefix: string, count: number): string[] {
  return Array.from({ length: count }, (_, index) => `${prefix}-${String(index + 1).padStart(2, "0")}`);
}

export function runCodeQueueSubmitSummaryContract(): JsonRecord {
  const submittedId = "codex_submitted_queued";
  const activeIds = manyIds("codex_running", 18);
  const response = compactSubmitSuccessResponseForTest({
    tasks: [task(submittedId, "queued")],
    queue: {
      counts: { running: 18, queued: 5, succeeded: 9 },
      activeTaskIds: [],
      queuedTaskIds: { items: [], count: 5, returned: 0, truncated: true },
      databaseActiveTaskIds: activeIds,
      databaseActiveTaskCount: activeIds.length,
      executionDiagnostics: {
        state: "healthy",
        databaseActiveTaskIds: activeIds,
        databaseActiveTaskCount: activeIds.length,
        schedulerActiveRunSlotCount: 0,
        schedulerActiveTaskIds: [],
        heartbeatFreshTaskIds: activeIds,
      },
    },
  }, { ok: true, status: 200 }, { mode: "local-atomic-directory-submit-serialization", acquiredAfterMs: 1, heldMs: 2, throttleMs: 2000 });

  const data = asRecord(response);
  const submitted = asRecord(data.submitted);
  const submittedTasks = asArray(submitted.tasks);
  const submittedTask = asRecord(submittedTasks[0]);
  const taskStates = asArray(submitted.taskStates);
  const submittedState = asRecord(taskStates[0]);
  const queue = asRecord(data.queue);
  const queuedTaskIds = asRecord(queue.queuedTaskIds);
  const activeTaskIds = asRecord(queue.activeTaskIds);
  const databaseActiveTaskIds = asRecord(queue.databaseActiveTaskIds);
  const submittedTaskIds = asRecord(queue.submittedTaskIds);
  const countContext = asRecord(queue.countContext);
  const listPreviewPolicy = asRecord(queue.listPreviewPolicy);
  const omittedCounts = asRecord(listPreviewPolicy.omittedCounts);
  const stateDisclosure = asRecord(queue.stateDisclosure);
  const activity = asRecord(queue.activity);
  const responseJson = JSON.stringify(response);

  assertCondition(submittedTask.id === submittedId && submittedTask.status === "queued", "submit response should keep the newly queued task", submittedTask);
  assertCondition(submittedState.id === submittedId && submittedState.status === "queued" && submittedState.state === "queued", "submitted task state should be explicit and authoritative", submittedState);
  assertCondition(String(submitted.stateSource || "").includes("response.tasks"), "submitted state source should point at response.tasks", submitted);
  assertCondition(asArray(submittedTaskIds.items).includes(submittedId), "submittedTaskIds should expose the just-submitted id", submittedTaskIds);
  assertCondition(asArray(queuedTaskIds.items).includes(submittedId), "queuedTaskIds preview should force-include the just-submitted queued task", queuedTaskIds);
  assertCondition(queuedTaskIds.count === 5 && queuedTaskIds.returned === 1 && queuedTaskIds.omitted === 4, "queuedTaskIds should preserve aggregate queued count without dumping all ids", queuedTaskIds);
  assertCondition(String(queuedTaskIds.source || "").includes("submittedTaskIds"), "queuedTaskIds source should explain submitted-task fallback", queuedTaskIds);
  assertCondition(String(queuedTaskIds.note || "").includes("count remains authoritative"), "queuedTaskIds should explain aggregate-count fallback", queuedTaskIds);

  assertCondition(asArray(activeTaskIds.items).length === 15, "activeTaskIds preview should stay bounded", activeTaskIds);
  assertCondition(activeTaskIds.count === 18 && activeTaskIds.omitted === 3 && activeTaskIds.truncated === true, "activeTaskIds should preserve active count and truncation", activeTaskIds);
  assertCondition(String(activeTaskIds.source || "").includes("databaseActiveTaskIds"), "activeTaskIds should fall back to database active ids when upstream activeTaskIds is empty", activeTaskIds);
  assertCondition(databaseActiveTaskIds.count === 18 && databaseActiveTaskIds.returned === 15, "databaseActiveTaskIds preview should preserve count context", databaseActiveTaskIds);
  assertCondition(countContext.running === 18 && countContext.active === 18 && countContext.databaseActive === 18, "countContext should expose accurate active counts", countContext);
  assertCondition(activity.effectiveActiveTaskCount === 18, "submit queue activity should expose commander effective active count", activity);

  assertCondition(listPreviewPolicy.bounded === true && listPreviewPolicy.countsAreAuthoritative === true, "list preview policy should document bounded low-noise output", listPreviewPolicy);
  assertCondition(listPreviewPolicy.truncated === true && omittedCounts.activeTaskIds === 3 && omittedCounts.queuedTaskIds === 4, "list preview policy should disclose omitted counts", listPreviewPolicy);
  assertCondition(String(listPreviewPolicy.emptyItemsSemantics || "").includes("idsUnavailable=true"), "list preview policy should document nonzero-count unavailable ids", listPreviewPolicy);
  assertCondition(String(stateDisclosure.submittedStatusSource || "").includes("response.tasks"), "stateDisclosure should name submitted status source", stateDisclosure);
  assertCondition(String(listPreviewPolicy.note || "").includes("Low-noise mutation output omits"), "list preview policy should include a clear truncation note", listPreviewPolicy);
  assertCondition(submitted.promptOmitted === true && !responseJson.includes("Focused submit summary contract"), "submit confirmation should not leak prompt text", response);
  assertCondition(responseJson.length < 12_000, "submit confirmation should remain low-noise", { chars: responseJson.length });

  const activeIdsOmitted = compactSubmitSuccessResponseForTest({
    tasks: [task("codex_submitted_queued_while_running", "queued", "live-fast-lane")],
    queue: {
      counts: { running: 9, queued: 1 },
      activeTaskIds: { items: [], count: 9, returned: 0, truncated: true },
      queuedTaskIds: { items: [], count: 1, returned: 0, truncated: true },
      databaseActiveTaskCount: 9,
      executionDiagnostics: {
        state: "split-brain",
        splitBrain: true,
        splitBrainLive: true,
        effectiveLiveness: "live",
        recommendedAction: "continue-supervision",
        databaseActiveTaskCount: 9,
        schedulerActiveRunSlotCount: 0,
        schedulerActiveTaskIds: [],
        activeHeartbeatCount: 9,
        heartbeatFreshTaskIds: [],
      },
    },
  }, { ok: true, status: 200 }, { mode: "local-atomic-directory-submit-serialization", acquiredAfterMs: 1, heldMs: 2, throttleMs: 2000 });
  const omittedQueue = asRecord(asRecord(activeIdsOmitted).queue);
  const omittedActive = asRecord(omittedQueue.activeTaskIds);
  const forcedQueued = asRecord(omittedQueue.queuedTaskIds);
  const omittedPolicy = asRecord(omittedQueue.listPreviewPolicy);
  const unavailable = asRecord(omittedPolicy.unavailableIdLists);
  assertCondition(omittedActive.count === 9 && omittedActive.returned === 0, "running-count nonzero should be preserved even with omitted active ids", omittedActive);
  assertNoItemsArrayWhenUnavailable(omittedActive, "activeTaskIds");
  assertCondition(asArray(forcedQueued.items).includes("codex_submitted_queued_while_running"), "new queued submitted task should be force-included even if upstream queued ids were omitted", forcedQueued);
  assertCondition(unavailable.activeTaskIds === true && unavailable.queuedTaskIds === false, "listPreviewPolicy should summarize unavailable active id list", unavailable);

  const liveSplitBrain = asRecord(omittedQueue.executionDiagnostics);
  const liveStateDisclosure = asRecord(omittedQueue.stateDisclosure);
  const liveActivity = asRecord(omittedQueue.activity);
  assertCondition(liveSplitBrain.splitBrainLive === true && liveSplitBrain.effectiveLiveness === "live", "split-brain-live heartbeat context should stay explicit", liveSplitBrain);
  assertCondition(liveActivity.splitBrainDisposition === "live-count-as-active", "split-brain-live should be counted as active in activity", liveActivity);
  assertCondition(String(liveStateDisclosure.splitBrainDisposition || "").includes("continue supervision"), "stateDisclosure should explain split-brain-live disposition", liveStateDisclosure);
  assertCondition(String(liveStateDisclosure.idsUnavailableMeaning || "").includes("not that there are no tasks"), "stateDisclosure should prevent empty-list misread", liveStateDisclosure);

  const transientRiskSubmit = compactSubmitSuccessResponseForTest({
    tasks: [task("codex_submitted_queued_during_stale_snapshot", "queued", "high-concurrency")],
    queue: {
      counts: { running: 7, queued: 1 },
      activeTaskIds: { items: [], count: 7, returned: 0, truncated: true },
      queuedTaskIds: { items: [], count: 1, returned: 0, truncated: true },
      databaseActiveTaskCount: 7,
      executionDiagnostics: {
        state: "stale-active",
        effectiveLiveness: "at-risk",
        recommendedAction: "investigate-heartbeat-risk",
        databaseActiveTaskCount: 7,
        databaseActiveTaskIds: manyIds("stale-db-active", 7),
        schedulerActiveRunSlotCount: 0,
        activeHeartbeatCount: 7,
        lastSchedulerHeartbeatAt: "2026-05-22T23:50:00.000Z",
        lastObservedAgentEventAt: "2026-05-22T23:49:30.000Z",
        heartbeatExpiredTaskIds: manyIds("stale-db-active", 7),
        staleRecoveryCandidateTaskIds: manyIds("stale-db-active", 7),
        heartbeatRiskTaskIds: manyIds("stale-db-active", 7),
      },
    },
  }, { ok: true, status: 200 }, { mode: "local-atomic-directory-submit-serialization", acquiredAfterMs: 1, heldMs: 2, throttleMs: 2000 });
  const transientQueue = asRecord(asRecord(transientRiskSubmit).queue);
  const transientActivity = asRecord(transientQueue.activity);
  const transientConcurrency = asRecord(transientQueue.commanderConcurrency);
  const transientRecovery = asRecord(transientActivity.recovery);
  const transientStateDisclosure = asRecord(transientQueue.stateDisclosure);
  const transientDiagnostics = asRecord(transientQueue.executionDiagnostics);
  const transientDiagnosticsRecovery = asRecord(transientDiagnostics.recovery);
  assertCondition(transientActivity.heartbeatRiskTaskCount === 7 && transientActivity.staleRecoveryCandidateTaskCount === 7, "submit activity should keep stale-active candidates visible", transientActivity);
  assertCondition(transientRecovery.disposition === "transient-needs-repoll", "submit stale snapshot should be classified as transient until re-polled", transientRecovery);
  assertCondition(transientRecovery.hint === "re-poll supervisor before recovery", "submit stale snapshot should emit the re-poll hint", transientRecovery);
  assertCondition(transientRecovery.boundedSnapshot === true && transientRecovery.snapshotRole === "submit-confirmation", "submit recovery context should identify bounded confirmation snapshot", transientRecovery);
  assertCondition(transientRecovery.lastObservedAgentEventBeforeSubmit === true, "submit recovery context should compare agent event time with submit time", transientRecovery);
  assertCondition(transientRecovery.recoveryMutationAllowedByThisSnapshot === false, "single submit snapshot must not allow recovery mutation", transientRecovery);
  assertCondition(transientConcurrency.attentionRequired === true && transientConcurrency.interventionRequired === false, "submit heartbeat risk should require attention but not direct high-risk intervention", transientConcurrency);
  assertCondition(transientStateDisclosure.transientRiskHint === "re-poll supervisor before recovery", "stateDisclosure should repeat low-noise re-poll hint", transientStateDisclosure);
  assertCondition(transientDiagnosticsRecovery.snapshotRole === "submit-confirmation" && transientDiagnosticsRecovery.hint === "re-poll supervisor before recovery", "submit execution diagnostics should carry submit snapshot recovery semantics", transientDiagnosticsRecovery);

  const queuedIdsOmitted = compactSubmitSuccessResponseForTest({
    tasks: [task("codex_submitted_already_running", "running", "live-fast-lane")],
    queue: {
      counts: { running: 1, queued: 3 },
      activeTaskIds: { items: [], count: 1, returned: 0, truncated: true },
      queuedTaskIds: { items: [], count: 3, returned: 0, truncated: true },
      executionDiagnostics: {
        state: "healthy",
        databaseActiveTaskCount: 1,
        databaseActiveTaskIds: ["codex_submitted_already_running"],
        activeHeartbeatCount: 1,
        heartbeatFreshTaskIds: ["codex_submitted_already_running"],
      },
    },
  }, { ok: true, status: 200 }, { mode: "local-atomic-directory-submit-serialization", acquiredAfterMs: 1, heldMs: 2, throttleMs: 2000 });
  const queuedOmittedQueue = asRecord(asRecord(queuedIdsOmitted).queue);
  const queuedOmitted = asRecord(queuedOmittedQueue.queuedTaskIds);
  const queuedOmittedPolicy = asRecord(queuedOmittedQueue.listPreviewPolicy);
  const queuedUnavailable = asRecord(queuedOmittedPolicy.unavailableIdLists);
  assertCondition(queuedOmitted.count === 3 && queuedOmitted.returned === 0, "queued-count nonzero should be preserved even with omitted queued ids", queuedOmitted);
  assertNoItemsArrayWhenUnavailable(queuedOmitted, "queuedTaskIds");
  assertCondition(queuedUnavailable.queuedTaskIds === true, "listPreviewPolicy should summarize unavailable queued id list", queuedUnavailable);

  return {
    ok: true,
    checks: [
      "newly queued submitted task is included in queuedTaskIds preview",
      "running count context falls back to database active ids",
      "nonzero count with omitted id lists uses idsUnavailable instead of items=[]",
      "split-brain-live submit summary says continue supervision and count as active",
      "stale submit snapshots preserve candidates but require re-poll before recovery",
      "bounded id previews disclose omitted counts",
      "submit confirmation omits prompt text and remains low-noise",
    ],
  };
}

if (import.meta.main) {
  process.stdout.write(`${JSON.stringify(runCodeQueueSubmitSummaryContract(), null, 2)}\n`);
}
