import type { RenderedCliResult } from "../output";
import { isRecord, stringValue } from "./config-utils";
import { renderTable } from "./render";
import type { RuntimeOptions } from "./runtime-options";

export function renderRuntimeMutationResult(ok: boolean, options: RuntimeOptions, parsed: Record<string, unknown> | null, exitCode: number): RenderedCliResult {
  const summary = parsed !== null && isRecord(parsed.summary) ? parsed.summary : {};
  const selection = parsed !== null && isRecord(parsed.selection) ? parsed.selection : {};
  const items = parsed !== null && Array.isArray(parsed.items) ? parsed.items.filter(isRecord) : [];
  const lines = [[
    "SUB2API RUNTIME", `operation=${options.action}`, `mode=${stringValue(parsed?.mode) ?? (options.confirm ? "confirmed" : "dry-run")}`,
    `ok=${ok ? "true" : "false"}`, `selected=${summary.selected ?? items.length}`, `changed=${summary.changed ?? "?"}`,
    `writeSucceeded=${summary.writeSucceeded ?? "?"}`, `writeFailed=${summary.writeFailed ?? "?"}`,
    `reconciled=${summary.reconciled ?? "?"}`, `mismatched=${summary.mismatched ?? "?"}`,
  ].join(" ")];
  if (typeof selection.mode === "string") {
    lines.push([
      "SELECTION", `mode=${selection.mode}`, `accountType=${selection.accountType ?? "-"}`,
      `candidates=${selection.candidateAccounts ?? selection.requestedSelectors ?? "?"}`,
      `configured=${selection.configuredPolicyMatches ?? "-"}`, `typeMatched=${selection.accountTypeMatches ?? "-"}`,
      `excludeRequested=${selection.explicitExclusionsRequested ?? 0}`, `excludedSelected=${selection.explicitlyExcluded ?? 0}`,
    ].join(" "));
  }
  const excludedAccounts = Array.isArray(selection.excludedAccounts) ? selection.excludedAccounts.filter(isRecord) : [];
  if (excludedAccounts.length > 0) lines.push(renderTable([
    ["EXCLUDED_ACCOUNT", "ID", "SELECTOR", "REASON"],
    ...excludedAccounts.map((item) => [
      stringValue(item.accountName) ?? "-", String(item.accountId ?? "-"),
      stringValue(item.selector) ?? "-", stringValue(item.reason) ?? "-",
    ]),
  ]));
  if (items.length > 0) {
    const fields = items.map((item) => ({
      item,
      write: isRecord(item.write) ? item.write : {},
      before: isRecord(item.before) ? item.before : {},
      desired: isRecord(item.desired) ? item.desired : {},
      actual: isRecord(item.actual) ? item.actual : {},
    }));
    if (options.kind === "priority") lines.push(renderTable([
      ["ACCOUNT", "ID", "SELECTED_BY", "CHANGE", "WRITE", "RECONCILED", "BEFORE_PRIORITY", "DESIRED_PRIORITY", "ACTUAL_PRIORITY", "FAILURE"],
      ...fields.map(({ item, write, before, desired, actual }) => [
        stringValue(item.accountName) ?? "-", String(item.accountId ?? "-"), stringValue(item.selectionReason) ?? "-", stringValue(item.change) ?? "-",
        stringValue(write.status) ?? "-", item.reconciled === true ? "true" : item.reconciled === false ? "false" : "-",
        String(before.priority ?? "-"), String(desired.priority ?? "-"), String(actual.priority ?? "-"),
        stringValue(write.error) ?? stringValue(item.reconcileError) ?? "-",
      ]),
    ]));
    else if (options.kind === "capacity") lines.push(renderTable([
      ["ACCOUNT", "ID", "SELECTED_BY", "CHANGE", "WRITE", "RECONCILED", "BEFORE_CAPACITY", "DESIRED_CAPACITY", "ACTUAL_CAPACITY", "FAILURE"],
      ...fields.map(({ item, write, before, desired, actual }) => [
        stringValue(item.accountName) ?? "-", String(item.accountId ?? "-"), stringValue(item.selectionReason) ?? "-", stringValue(item.change) ?? "-",
        stringValue(write.status) ?? "-", item.reconciled === true ? "true" : item.reconciled === false ? "false" : "-",
        String(before.capacity ?? "-"), String(desired.capacity ?? "-"), String(actual.capacity ?? "-"),
        stringValue(write.error) ?? stringValue(item.reconcileError) ?? "-",
      ]),
    ]));
    else if (options.kind === "account-name") lines.push(renderTable([
      ["ACCOUNT", "ID", "SELECTED_BY", "CHANGE", "WRITE", "RECONCILED", "BEFORE_NAME", "DESIRED_NAME", "ACTUAL_NAME", "FAILURE"],
      ...fields.map(({ item, write, before, desired, actual }) => [
        stringValue(item.accountName) ?? "-", String(item.accountId ?? "-"), stringValue(item.selectionReason) ?? "-", stringValue(item.change) ?? "-",
        stringValue(write.status) ?? "-", item.reconciled === true ? "true" : item.reconciled === false ? "false" : "-",
        stringValue(before.name) ?? "-", stringValue(desired.name) ?? "-", stringValue(actual.name) ?? "-",
        stringValue(write.error) ?? stringValue(item.reconcileError) ?? "-",
      ]),
    ]));
    else lines.push(renderTable([
      ["ACCOUNT", "ID", "SELECTED_BY", "CHANGE", "WRITE", "RECONCILED", "BEFORE", "DESIRED", "ACTUAL", "BEFORE_CODES", "DESIRED_CODES", "ACTUAL_CODES", "FAILURE"],
      ...fields.map(({ item, write, before, desired, actual }) => {
        const codes = (value: unknown): string => Array.isArray(value) ? value.join(",") || "-" : "-";
        return [
          stringValue(item.accountName) ?? "-", String(item.accountId ?? "-"), stringValue(item.selectionReason) ?? "-", stringValue(item.change) ?? "-",
          stringValue(write.status) ?? "-", item.reconciled === true ? "true" : item.reconciled === false ? "false" : "-",
          String(before.ruleCount ?? "-"), String(desired.ruleCount ?? "-"), String(actual.ruleCount ?? "-"),
          codes(before.statusCodes), codes(desired.statusCodes), codes(actual.statusCodes),
          stringValue(write.error) ?? stringValue(item.reconcileError) ?? "-",
        ];
      }),
    ]));
  } else if (parsed !== null && typeof parsed.error === "string") lines.push(`ERROR ${parsed.error}`);
  else if (parsed === null) lines.push(`ERROR runtime output unavailable remote_exit=${exitCode}`);
  return { ok, command: "platform-infra sub2api codex-pool runtime", renderedText: lines.join("\n"), contentType: "text/plain", projection: parsed ?? undefined };
}
