Skip to content

"Vibe-Trading: Your Personal Trading Agent"

26.6 KB
Raw Blame
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { RunnerBudgetControls } from "@/components/chat/RunnerBudgetControls";
import type { ReactNode } from "react";
import { useTranslation } from "react-i18next";
import type { TFunction } from "i18next";
import {
  Activity,
  AlertTriangle,
  CheckCircle2,
  Clock3,
  Loader2,
  OctagonX,
  RefreshCw,
  ShieldCheck,
  ShieldOff,
  Wifi,
  WifiOff,
} from "lucide-react";
import { api, type LiveBrokerStatus, type LiveMandateLimits, type LiveStatus } from "@/lib/api";
import { cn } from "@/lib/utils";

const RUNTIME_POLL_INTERVAL_MS = 15_000;
const RUNTIME_CLOCK_INTERVAL_MS = 1_000;

export function Runtime() {
  const { t } = useTranslation();
  const [status, setStatus] = useState<LiveStatus | null>(null);
  const [loading, setLoading] = useState(true);
  const [refreshing, setRefreshing] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [nowMs, setNowMs] = useState(() => Date.now());
  const activeRequestRef = useRef<{ id: number; controller: AbortController } | null>(null);
  const requestSeqRef = useRef(0);
  const mountedRef = useRef(false);
  const tRef = useRef(t);

  useEffect(() => {
    tRef.current = t;
  }, [t]);

  const loadStatus = useCallback(async (mode: "initial" | "refresh" = "refresh") => {
    const requestId = requestSeqRef.current + 1;
    requestSeqRef.current = requestId;
    activeRequestRef.current?.controller.abort();
    const controller = new AbortController();
    activeRequestRef.current = { id: requestId, controller };

    if (mode === "initial") setLoading(true);
    else setRefreshing(true);
    setError(null);
    try {
      const next = await api.getLiveStatus(controller.signal);
      if (!mountedRef.current || !isCurrentStatusRequest(activeRequestRef.current, requestId, controller)) return;
      setStatus(next);
    } catch (err) {
      if (controller.signal.aborted) return;
      if (!mountedRef.current || !isCurrentStatusRequest(activeRequestRef.current, requestId, controller)) return;
      console.warn("Failed to load runtime status", err);
      setStatus(null);
      setError(err instanceof Error ? err.message : tRef.current("runtime.statusUnavailable"));
    } finally {
      if (!mountedRef.current || !isCurrentStatusRequest(activeRequestRef.current, requestId, controller)) return;
      activeRequestRef.current = null;
      setLoading(false);
      setRefreshing(false);
    }
  }, []);

  useEffect(() => {
    mountedRef.current = true;
    loadStatus("initial");
    const pollTimer = window.setInterval(() => loadStatus("refresh"), RUNTIME_POLL_INTERVAL_MS);
    const clockTimer = window.setInterval(() => setNowMs(Date.now()), RUNTIME_CLOCK_INTERVAL_MS);
    return () => {
      mountedRef.current = false;
      requestSeqRef.current += 1;
      activeRequestRef.current?.controller.abort();
      activeRequestRef.current = null;
      window.clearInterval(pollTimer);
      window.clearInterval(clockTimer);
    };
  }, [loadStatus]);

  const summary = useMemo(() => summarizeRuntime(status), [status]);
  const sortedBrokers = useMemo(() => sortBrokersByStatus(status), [status]);

  return (
    <div className="min-h-screen p-6 lg:p-8">
      <div className="mx-auto flex w-full max-w-6xl flex-col gap-6">
        <section className="flex flex-col gap-4 border-b border-border/60 pb-6 lg:flex-row lg:items-end lg:justify-between">
          <div className="space-y-3">
            <div className="inline-flex items-center gap-2 rounded-md border border-border/60 px-2.5 py-1 text-xs font-medium text-muted-foreground">
              <Activity className="h-3.5 w-3.5" />
              {t("runtime.monitorBadge")}
            </div>
            <div>
              <h1 className="text-2xl font-semibold tracking-tight">{t("runtime.title")}</h1>
              <p className="mt-2 max-w-2xl text-sm text-muted-foreground">
                {t("runtime.subtitlePre")}
                {t("runtime.subtitlePost")}
              </p>
            </div>
          </div>
          <button
            type="button"
            onClick={() => loadStatus("refresh")}
            disabled={refreshing}
            className="inline-flex items-center gap-2 rounded-md border border-border/60 px-4 py-2 text-sm font-medium transition hover:bg-muted/60 disabled:opacity-50"
          >
            {refreshing ? <Loader2 className="h-4 w-4 animate-spin" /> : <RefreshCw className="h-4 w-4" />}
            {t("runtime.refresh")}
          </button>
        </section>

        {loading ? (
          <div className="grid gap-3 md:grid-cols-4">
            {[1, 2, 3, 4].map((item) => (
              <div key={item} className="h-24 animate-pulse rounded-xl border border-border/60 bg-card shadow-sm" />
            ))}
          </div>
        ) : null}

        {!loading && error ? (
          <section className="rounded-xl border border-warning/30 bg-warning/5 p-5 shadow-sm">
            <div className="flex items-center gap-2 font-medium text-warning">
              <AlertTriangle className="h-5 w-5" />
              {t("runtime.unavailableTitle")}
            </div>
            <p className="mt-2 text-sm text-muted-foreground">{error}</p>
            <p className="mt-2 text-xs text-muted-foreground">{t("runtime.unavailableHint")}</p>
          </section>
        ) : null}

        {!loading && !error && status ? (
          <>
            <section className="grid gap-3 md:grid-cols-4">
              <SummaryTile
                label={t("runtime.globalHalt")}
                value={status.global_halted ? t("runtime.halted") : t("runtime.clear")}
                tone={status.global_halted ? "danger" : "success"}
                icon={status.global_halted ? OctagonX : CheckCircle2}
              />
              <SummaryTile label={t("runtime.brokers")} value={String(summary.brokerCount)} tone="neutral" icon={Activity} />
              <SummaryTile
                label={t("runtime.authorized")}
                value={String(summary.authorizedCount)}
                tone={summary.authorizedCount > 0 ? "success" : "neutral"}
                icon={summary.authorizedCount > 0 ? Wifi : WifiOff}
              />
              <SummaryTile
                label={t("runtime.runners")}
                value={t("runtime.running", { count: summary.runningCount })}
                tone={summary.runningCount > 0 && !status.global_halted ? "success" : "neutral"}
                icon={summary.runningCount > 0 ? Activity : Clock3}
              />
            </section>

            {status.brokers.length === 0 ? (
              <section className="rounded-xl border border-dashed border-border/60 bg-card p-5 text-center shadow-sm">
                <ShieldOff className="mx-auto h-8 w-8 text-muted-foreground" />
                <h2 className="mt-3 text-sm font-semibold">{t("runtime.noProfilesTitle")}</h2>
                <p className="mt-1 text-sm text-muted-foreground">{t("runtime.noProfilesBody")}</p>
              </section>
            ) : (
              <section className="grid gap-4">
                {sortedBrokers.map((broker) => (
                  <BrokerRuntimeCard
                    key={broker.auth.profile_id || broker.auth.broker}
                    broker={broker}
                    globalHalted={status.global_halted}
                    t={t}
                    nowMs={nowMs}
                    onRefresh={() => loadStatus("refresh")}
                  />
                ))}
              </section>
            )}
          </>
        ) : null}
      </div>
    </div>
  );
}

interface SummaryTileProps {
  label: string;
  value: string;
  tone: "success" | "danger" | "neutral";
  icon: typeof Activity;
}

function isCurrentStatusRequest(
  activeRequest: { id: number; controller: AbortController } | null,
  requestId: number,
  controller: AbortController,
): boolean {
  return activeRequest?.id === requestId && activeRequest.controller === controller;
}

function SummaryTile({ label, value, tone, icon: Icon }: SummaryTileProps) {
  return (
    <div className="rounded-xl border border-border/60 bg-card p-4 shadow-sm">
      <div className="flex items-center justify-between gap-3">
        <span className="text-[11px] font-medium uppercase tracking-wide text-muted-foreground">{label}</span>
        <Icon
          className={cn(
            "h-4 w-4",
            tone === "success" && "text-success",
            tone === "danger" && "text-danger",
            tone === "neutral" && "text-muted-foreground",
          )}
        />
      </div>
      <div
        className={cn(
          "mt-3 text-2xl font-semibold",
          tone === "success" && "text-success",
          tone === "danger" && "text-danger",
        )}
      >
        {value}
      </div>
    </div>
  );
}

function BrokerRuntimeCard({
  broker,
  globalHalted,
  t,
  nowMs,
  onRefresh,
}: {
  broker: LiveBrokerStatus;
  globalHalted: boolean;
  t: TFunction;
  nowMs: number;
  onRefresh: () => Promise<void>;
}) {
  const brokerKey = broker.auth.broker;
  const runnerAlive = broker.runner?.alive ?? false;
  const halted = globalHalted || broker.halted;
  const mandate = broker.mandate ?? null;
  const risk = deriveRiskState(broker, globalHalted, t);
  const mandateCountdown = formatCountdown(mandate?.expires_at, t, nowMs);

  if (broker.auth.transport === "broker_sdk") {
    return <SdkBrokerRuntimeCard broker={broker} t={t} onRefresh={onRefresh} />;
  }

  return (
    <article className="rounded-xl border border-border/60 bg-card p-4 shadow-sm">
      <div className="flex flex-col gap-3 lg:flex-row lg:items-start lg:justify-between">
        <div>
          <div className="flex flex-wrap items-center gap-2">
            <h2 className="text-sm font-semibold capitalize">{brokerKey}</h2>
            <StatusPill
              label={broker.auth.oauth_token_present ? t("runtime.authPresent") : t("runtime.authMissing")}
              tone={broker.auth.oauth_token_present ? "success" : "neutral"}
            />
            <StatusPill
              label={runnerAlive ? t("runtime.runnerAlive") : t("runtime.runnerStopped")}
              tone={runnerAlive ? "success" : "neutral"}
            />
            {halted ? <StatusPill label={t("runtime.haltedPill")} tone="danger" /> : null}
          </div>
          <p className="mt-2 text-sm text-muted-foreground">
            {broker.auth.is_live_broker ? t("runtime.recognizedProfile") : t("runtime.unknownProfile")} · {t("runtime.lastTick")}{" "}
            {formatLastTick(broker.runner?.last_tick, broker.runner?.last_tick_age_seconds, t, nowMs)}
          </p>
        </div>
        <StatusPill label={risk.label} tone={risk.tone} />
      </div>

      <div className="mt-4 grid gap-3 md:grid-cols-3">
        <RuntimePanel title={t("runtime.authorization")} icon={broker.auth.oauth_token_present ? Wifi : WifiOff}>
          <KeyValue label={t("runtime.oauthToken")} value={broker.auth.oauth_token_present ? t("runtime.present") : t("runtime.missing")} />
          <KeyValue label={t("runtime.profileType")} value={broker.auth.is_live_broker ? t("runtime.recognized") : t("runtime.unknown")} />
        </RuntimePanel>

        <RuntimePanel title={t("runtime.mandate")} icon={mandate ? ShieldCheck : ShieldOff}>
          {mandate ? (
            <>
              <KeyValue label={t("runtime.account")} value={mandate.account_ref || t("runtime.unrecorded")} />
              <KeyValue label={t("runtime.expiry")} value={mandate.expired ? t("runtime.expired") : mandateCountdown} />
              <KeyValue label={t("runtime.limits")} value={summarizeLimits(mandate.limits, t)} />
            </>
          ) : (
            <p className="text-sm text-muted-foreground">{t("runtime.noMandate")}</p>
          )}
        </RuntimePanel>

        <RuntimePanel title={t("runtime.riskStateTitle")} icon={risk.icon}>
          <p className="text-sm text-muted-foreground">{risk.description}</p>
        </RuntimePanel>
      </div>
      <div className="mt-3"><RunnerBudgetControls broker={brokerKey} budget={broker.decision_budget} running={runnerAlive} onRefresh={() => { void onRefresh(); }} /></div>
    </article>
  );
}

function SdkBrokerRuntimeCard({ broker, t, onRefresh }: { broker: LiveBrokerStatus; t: TFunction; onRefresh: () => Promise<void> }) {
  const auth = broker.auth;
  const [verifying, setVerifying] = useState(false);
  const [verifyError, setVerifyError] = useState<string | null>(null);
  const profileId = auth.profile_id || `${auth.broker}-live-sdk-readonly`;
  const state = connectorState(auth, t);

  const verify = useCallback(async () => {
    if (verifying) return;
    setVerifying(true);
    setVerifyError(null);
    try {
      await api.verifyConnector(profileId);
      await onRefresh();
    } catch {
      setVerifyError(t("runtime.connectorVerifyFailed"));
    } finally {
      setVerifying(false);
    }
  }, [onRefresh, profileId, t, verifying]);

  return (
    <article className="rounded-xl border border-border/60 bg-card p-4 shadow-sm">
      <div className="flex flex-col gap-3 lg:flex-row lg:items-start lg:justify-between">
        <div>
          <div className="flex flex-wrap items-center gap-2">
            <h2 className="text-sm font-semibold capitalize">{auth.broker}</h2>
            <StatusPill label={state.label} tone={state.tone} />
          </div>
          {isReadOnlyCompatible(auth) ? (
            <p className="mt-2 text-sm text-muted-foreground">{t("runtime.sdkConnectorProfile")}</p>
          ) : null}
        </div>
        {state.action ? (
          <button
            type="button"
            onClick={verify}
            disabled={verifying}
            className="inline-flex items-center gap-2 rounded-md border border-border/60 px-4 py-2 text-sm font-medium transition hover:bg-muted/60 disabled:opacity-50"
          >
            {verifying ? <Loader2 className="h-4 w-4 animate-spin" /> : <RefreshCw className="h-4 w-4" />}
            {t(state.action)}
          </button>
        ) : null}
      </div>

      {state.kind === "not_configured" ? (
        <ConnectorMissingSetup broker={auth.broker} t={t} />
      ) : (
        <div className="mt-4 grid gap-3 md:grid-cols-3">
          <RuntimePanel title={t("runtime.connectionDetails")} icon={state.kind === "connected" ? Wifi : WifiOff}>
            <KeyValue label={t("runtime.credentialSource")} value={auth.credential_source || t("runtime.unknown")} />
            <KeyValue label={t("runtime.sdk")} value={formatSdkState(auth.sdk_installed, t)} />
          </RuntimePanel>
          <RuntimePanel title={t("runtime.environment")} icon={ShieldCheck}>
            <KeyValue label={t("runtime.environmentIdentity")} value={formatEnvironmentIdentity(auth.environment_identity, t)} />
            <KeyValue label={t("runtime.capabilities")} value={formatCapabilities(auth, t)} />
          </RuntimePanel>
          <RuntimePanel title={t("runtime.diagnostics")} icon={state.kind === "error" ? AlertTriangle : CheckCircle2}>
            <KeyValue label={t("runtime.lastChecked")} value={auth.last_checked_at || t("runtime.never")} />
            {auth.error_code ? <KeyValue label={t("runtime.errorCode")} value={auth.error_code} /> : null}
            {state.kind === "error" ? <p className="text-sm text-muted-foreground">{connectorDiagnostic(auth.error_code, t)}</p> : null}
          </RuntimePanel>
        </div>
      )}
      {verifyError ? <p role="alert" className="mt-3 text-sm text-danger">{verifyError}</p> : null}
    </article>
  );
}

type ConnectorKind = "not_configured" | "ready" | "connected" | "error" | "unknown";

function connectorKind(auth: LiveBrokerStatus["auth"]): ConnectorKind {
  if (auth.connection_state === "connected") return "connected";
  if (auth.connection_state === "not_configured" || auth.configured === false) return "not_configured";
  if (auth.connection_state === "error") return "error";
  if (auth.connection_state === "ready") return "ready";
  return "unknown";
}

function connectorState(auth: LiveBrokerStatus["auth"], t: TFunction): {
  kind: ConnectorKind;
  label: string;
  tone: "success" | "danger" | "warning" | "neutral";
  action?: "runtime.verifyConnection" | "runtime.retry";
} {
  const kind = connectorKind(auth);
  if (kind === "connected") {
    if (isReadOnlyCompatible(auth)) {
      return { kind, label: t("runtime.connectedReadOnly"), tone: "success" };
    }
    return { kind, label: t("runtime.connectedAccessUnknown"), tone: "neutral" };
  }
  if (kind === "not_configured") {
    return { kind, label: t("runtime.notConfigured"), tone: "neutral" };
  }
  if (kind === "error") {
    return { kind, label: t("runtime.connectionFailed"), tone: "danger", action: "runtime.retry" };
  }
  if (kind === "ready") {
    return { kind, label: t("runtime.readyToVerify"), tone: "warning", action: "runtime.verifyConnection" };
  }
  return { kind, label: t("runtime.connectorStatusUnavailable"), tone: "neutral" };
}

function connectorDiagnostic(errorCode: string | null | undefined, t: TFunction): string {
  switch (errorCode) {
    case "credentials_partial": return t("runtime.diagnosticCredentialsPartial");
    case "credentials_conflict": return t("runtime.diagnosticCredentialsConflict");
    case "sdk_missing": return t("runtime.diagnosticSdkMissing");
    case "authentication_failed": return t("runtime.diagnosticAuthenticationFailed");
    case "network_unreachable": return t("runtime.diagnosticNetworkUnreachable");
    default: return t("runtime.diagnosticBrokerError");
  }
}

function formatSdkState(installed: boolean | null | undefined, t: TFunction): string {
  if (installed === true) return t("runtime.installed");
  if (installed === false) return t("runtime.notInstalled");
  return t("runtime.unknown");
}

function formatEnvironmentIdentity(identity: string | null | undefined, t: TFunction): string {
  if (identity === "config_declared" || identity === "config-declared") return t("runtime.configDeclared");
  if (identity === "path_separated_key_bound") return t("runtime.pathSeparatedKeyBound");
  return identity || t("runtime.unknown");
}

const SDK_CONNECTOR_SETUP_HINTS = {
  longbridge: {
    introKey: "runtime.missingLongbridgeVariables",
    variables: ["LONGBRIDGE_APP_KEY", "LONGBRIDGE_APP_SECRET", "LONGBRIDGE_ACCESS_TOKEN"],
  },
  etoro: {
    introKey: "runtime.missingEtoroVariables",
    variables: ["ETORO_API_KEY", "ETORO_USER_KEY"],
    fileHintKey: "runtime.missingEtoroFileHint",
  },
} as const;

function ConnectorMissingSetup({ broker, t }: { broker: string; t: TFunction }) {
  const hints =
    SDK_CONNECTOR_SETUP_HINTS[broker as keyof typeof SDK_CONNECTOR_SETUP_HINTS];
  if (!hints) {
    return (
      <section className="mt-4 rounded-xl border border-dashed border-border/60 bg-muted/40 p-4 shadow-sm">
        <p className="text-sm text-muted-foreground">{t("runtime.missingConnectorVariables")}</p>
      </section>
    );
  }
  return (
    <section className="mt-4 rounded-xl border border-dashed border-border/60 bg-muted/40 p-4 shadow-sm">
      <p className="text-sm text-muted-foreground">{t(hints.introKey)}</p>
      <ul className="mt-2 grid gap-1 font-mono text-sm">
        {hints.variables.map((variable) => (
          <li key={variable}>{variable}</li>
        ))}
      </ul>
      {"fileHintKey" in hints && hints.fileHintKey ? (
        <p className="mt-2 text-sm text-muted-foreground">{t(hints.fileHintKey)}</p>
      ) : null}
    </section>
  );
}

function isReadCapability(capability: string): boolean {
  return capability.endsWith(".read");
}

function isReadOnlyCompatible(auth: LiveBrokerStatus["auth"]): boolean {
  if (auth.connection_state !== "connected") return false;
  if (auth.readonly !== true) return false;
  if (!auth.profile_id?.endsWith("-readonly")) return false;
  if (!auth.capabilities?.length) return false;
  return auth.capabilities.every(isReadCapability);
}

function formatCapabilities(auth: LiveBrokerStatus["auth"], t: TFunction): string {
  const labels: Record<string, string> = {
    "account.read": t("runtime.capabilityAccount"),
    "positions.read": t("runtime.capabilityPositions"),
    "orders.read": t("runtime.capabilityOpenOrders"),
    "quotes.read": t("runtime.capabilityQuotes"),
    "history.read": t("runtime.capabilityHistory"),
  };
  const readCapabilities = auth.capabilities?.filter(isReadCapability) ?? [];
  const rendered = readCapabilities.map((capability) => labels[capability] || capability).join(", ");
  if (!isReadOnlyCompatible(auth)) {
    return rendered ? `${rendered} · ${t("runtime.accessUnknown")}` : t("runtime.accessUnknown");
  }
  return `${rendered} · ${t("runtime.readOnly")}`;
}

function RuntimePanel({ title, icon: Icon, children }: { title: string; icon: typeof Activity; children: ReactNode }) {
  return (
    <section className="rounded-xl border border-border/60 bg-muted/40 p-4 shadow-sm">
      <div className="mb-3 flex items-center gap-2 text-[11px] font-semibold uppercase tracking-wide text-muted-foreground">
        <Icon className="h-3.5 w-3.5" />
        {title}
      </div>
      <div className="space-y-2">{children}</div>
    </section>
  );
}

function KeyValue({ label, value }: { label: string; value: string }) {
  return (
    <div>
      <div className="text-[11px] uppercase text-muted-foreground">{label}</div>
      <div className="font-mono text-sm">{value || "-"}</div>
    </div>
  );
}

function StatusPill({ label, tone }: { label: string; tone: "success" | "danger" | "warning" | "neutral" }) {
  return (
    <span
      className={cn(
        "inline-flex items-center rounded px-2 py-0.5 text-xs font-medium",
        tone === "success" && "bg-success/10 text-success",
        tone === "danger" && "bg-danger/10 text-danger",
        tone === "warning" && "bg-warning/10 text-warning",
        tone === "neutral" && "bg-muted text-muted-foreground",
      )}
    >
      {label}
    </span>
  );
}

function summarizeRuntime(status: LiveStatus | null) {
  const brokers = status?.brokers || [];
  return {
    brokerCount: brokers.length,
    authorizedCount: brokers.filter(
      (broker) => broker.auth.oauth_token_present || broker.auth.connection_state === "connected",
    ).length,
    runningCount: brokers.filter((broker) => broker.runner?.alive).length,
  };
}

function connectorRank(broker: LiveBrokerStatus, globalHalted: boolean): number {
  const auth = broker.auth;
  if (auth.transport === "broker_sdk") {
    const kind = connectorKind(auth);
    if (kind === "connected") return 0;
    if (kind === "error") return 1;
    if (kind === "ready") return 2;
    return auth.sdk_installed === true ? 3 : 4;
  }
  if (auth.oauth_token_present || broker.runner?.alive || broker.mandate) return 0;
  // `halted` is true for every broker while the global kill switch is set, so a broker's
  // own switch is only visible when the global one is clear.
  return broker.halted && !globalHalted ? 1 : 4;
}

function sortBrokersByStatus(status: LiveStatus | null): LiveBrokerStatus[] {
  const globalHalted = status?.global_halted ?? false;
  // Stable sort: peers keep the backend's alphabetical order, so a refresh never reshuffles them.
  return [...(status?.brokers || [])].sort((a, b) => connectorRank(a, globalHalted) - connectorRank(b, globalHalted));
}

function deriveRiskState(broker: LiveBrokerStatus, globalHalted: boolean, t: TFunction): {
  label: string;
  tone: "success" | "danger" | "warning" | "neutral";
  icon: typeof Activity;
  description: string;
} {
  if (globalHalted || broker.halted) {
    return {
      label: t("runtime.riskHalted"),
      tone: "danger",
      icon: OctagonX,
      description: t("runtime.riskHaltedDesc"),
    };
  }
  if (broker.runner?.alive && broker.mandate && !broker.mandate.expired) {
    return {
      label: t("runtime.riskActive"),
      tone: "success",
      icon: Activity,
      description: t("runtime.riskActiveDesc"),
    };
  }
  if (broker.auth.oauth_token_present && broker.mandate && !broker.mandate.expired) {
    return {
      label: t("runtime.riskIdle"),
      tone: "warning",
      icon: Clock3,
      description: t("runtime.riskIdleDesc"),
    };
  }
  return {
    label: t("runtime.riskDormant"),
    tone: "neutral",
    icon: ShieldOff,
    description: t("runtime.riskDormantDesc"),
  };
}

function summarizeLimits(limits: LiveMandateLimits | undefined, t: TFunction): string {
  if (!limits) return t("runtime.limitsUnavailable");
  const parts: string[] = [];
  if (typeof limits.max_order_notional_usd === "number") parts.push(`${formatUsd(limits.max_order_notional_usd)}${t("runtime.perOrder")}`);
  if (typeof limits.max_total_exposure_usd === "number") parts.push(`${formatUsd(limits.max_total_exposure_usd)} ${t("runtime.exposure")}`);
  if (typeof limits.max_trades_per_day === "number") parts.push(`${limits.max_trades_per_day}${t("runtime.perDay")}`);
  if (typeof limits.max_leverage === "number") parts.push(`${limits.max_leverage}${t("runtime.leverageSuffix")}`);
  if (limits.allowed_instruments?.length) parts.push(limits.allowed_instruments.join(", "));
  return parts.join(" · ") || t("runtime.limitsUnavailable");
}

function formatUsd(value: number): string {
  return `$${value.toLocaleString("en-US", { maximumFractionDigits: 0 })}`;
}

function formatCountdown(iso: string | undefined, t: TFunction, nowMs: number): string {
  if (!iso) return t("runtime.unknown");
  const target = new Date(iso).getTime();
  if (!Number.isFinite(target)) return t("runtime.unknown");
  const deltaSec = Math.round((target - nowMs) / 1000);
  if (deltaSec <= 0) return t("runtime.expired");
  const days = Math.floor(deltaSec / 86_400);
  const hours = Math.floor((deltaSec % 86_400) / 3600);
  if (days > 0) return `${days}d ${hours}h`;
  if (hours > 0) return `${hours}h`;
  if (deltaSec < 60) return `${deltaSec}s`;
  return `${Math.floor(deltaSec / 60)}m`;
}

function formatLastTick(
  value: string | number | null | undefined,
  ageSeconds: number | null | undefined,
  t: TFunction,
  nowMs: number,
): string {
  if (typeof ageSeconds === "number" && Number.isFinite(ageSeconds)) {
    if (ageSeconds < 60) return `${Math.round(ageSeconds)}s ${t("runtime.ago")}`;
    if (ageSeconds < 3600) return `${Math.floor(ageSeconds / 60)}m ${t("runtime.ago")}`;
    return `${Math.floor(ageSeconds / 3600)}h ${t("runtime.ago")}`;
  }
  if (value == null || value === "") return t("runtime.never");
  const timestamp = typeof value === "number" ? normalizeEpochMs(value) : new Date(value).getTime();
  if (!Number.isFinite(timestamp)) return t("runtime.unknown");
  const deltaSec = Math.round((nowMs - timestamp) / 1000);
  if (deltaSec < 60) return `${Math.max(0, deltaSec)}s ${t("runtime.ago")}`;
  if (deltaSec < 3600) return `${Math.floor(deltaSec / 60)}m ${t("runtime.ago")}`;
  return `${Math.floor(deltaSec / 3600)}h ${t("runtime.ago")}`;
}

function normalizeEpochMs(value: number): number {
  if (value >= 1_000_000_000_000) return value;
  if (value >= 946_684_800 && value <= 4_102_444_800) return value * 1000;
  return Number.NaN;
}