/**
 * GET /api/v3/protocol/status
 *
 * Fail-closed default route. Production must inject live chain, governance,
 * pause, and kill-switch readers before this endpoint may return HTTP 200.
 * Until then the versioned V3 envelope exposes source errors and null data,
 * rather than manufacturing healthy/zero-default state.
 */

import {
  createV3EnvelopeResponse,
  type V3Envelope,
  V3_ENVELOPE_SCHEMA_VERSION,
} from "../../compat/v3-envelope";

export const STATUS_SCHEMA_VERSION = V3_ENVELOPE_SCHEMA_VERSION;

export type StatusChain = {
  name: "base" | "avalanche" | "arbitrum" | "polygon";
  chainId: number;
  status: "unknown";
  finality: null;
  killSwitch: null;
  dataSourceErrors: string[];
};

/** Retained for injected live readers; it is never returned as a healthy default. */
export type ProtocolStatusData = {
  chains: StatusChain[];
  v3: {
    pausedContracts: string[];
    guardianAddress: `0x${string}` | null;
    multisigAddress: `0x${string}` | null;
    timelockSeconds: number | null;
  };
  v2: { pausedContracts: string[] };
};

export type ProtocolStatusReport = V3Envelope<ProtocolStatusData>;

const UNCONFIGURED = "live-status-reader-unconfigured";

export function createUnconfiguredStatusReport(now: Date = new Date()): ProtocolStatusReport {
  return {
    chainId: null,
    contractAddress: null,
    protocolVersion: "v3",
    contractStatus: "unknown",
    status: "degraded",
    blockNumber: null,
    blockHash: null,
    observedAt: now.toISOString(),
    finality: null,
    dataSourceErrors: [UNCONFIGURED],
    schemaVersion: STATUS_SCHEMA_VERSION,
    data: null,
  };
}

export async function GET(_request: Request): Promise<Response> {
  return createV3EnvelopeResponse(createUnconfiguredStatusReport());
}
