import type { GetStaticProps } from 'next'
import OperationsHubPage from '@/components/explorer/OperationsHubPage'
import type { MissionControlBridgeStatusResponse } from '@/services/api/missionControl'
import type { RouteMatrixResponse } from '@/services/api/routes'
import type { CapabilitiesResponse, NetworksConfigResponse, TokenListResponse } from '@/services/api/config'
import type { ExplorerStats } from '@/services/api/stats'
import { fetchPublicJson } from '@/utils/publicExplorer'
import { fetchExplorerTruthContext } from '@/utils/serverExplorerContext'
import { loadTokenListResponseForSurface } from '@/services/api/tokenListSurfaces'

interface OperationsPageProps {
  initialBridgeStatus: MissionControlBridgeStatusResponse | null
  initialRouteMatrix: RouteMatrixResponse | null
  initialNetworksConfig: NetworksConfigResponse | null
  initialTokenList: TokenListResponse | null
  initialCapabilities: CapabilitiesResponse | null
  initialStats: ExplorerStats | null
}

export default function OperationsPage(props: OperationsPageProps) {
  return <OperationsHubPage {...props} />
}

export const getStaticProps: GetStaticProps<OperationsPageProps> = async () => {
  const [routesResult, networksResult, tokenListResult, capabilitiesResult, truthContext] = await Promise.all([
    fetchPublicJson<RouteMatrixResponse>('/token-aggregation/api/v1/routes/matrix?includeNonLive=true').catch(() => null),
    fetchPublicJson<NetworksConfigResponse>('/api/config/networks').catch(() => null),
    loadTokenListResponseForSurface('extended', 138).then((value) => value.response).catch(() => null),
    fetchPublicJson<CapabilitiesResponse>('/api/config/capabilities').catch(() => null),
    fetchExplorerTruthContext(),
  ])

  return {
    props: {
      initialBridgeStatus: truthContext.initialBridgeStatus,
      initialRouteMatrix: routesResult,
      initialNetworksConfig: networksResult,
      initialTokenList: tokenListResult,
      initialCapabilities: capabilitiesResult,
      initialStats: truthContext.initialStats,
    },
  }
}
