import type { GetStaticProps } from 'next'
import RoutesMonitoringPage from '@/components/explorer/RoutesMonitoringPage'
import type { ExplorerStats } from '@/services/api/stats'
import type { MissionControlBridgeStatusResponse } from '@/services/api/missionControl'
import { fetchPublicJson } from '@/utils/publicExplorer'
import { fetchExplorerTruthContext } from '@/utils/serverExplorerContext'
import type {
  ExplorerNetwork,
  MissionControlLiquidityPool,
  RouteMatrixResponse,
} from '@/services/api/routes'

interface RoutesPageProps {
  initialRouteMatrix: RouteMatrixResponse | null
  initialNetworks: ExplorerNetwork[]
  initialPools: MissionControlLiquidityPool[]
  initialStats: ExplorerStats | null
  initialBridgeStatus: MissionControlBridgeStatusResponse | null
}

export default function RoutesPage(props: RoutesPageProps) {
  return <RoutesMonitoringPage {...props} />
}

export const getStaticProps: GetStaticProps<RoutesPageProps> = async () => {
  const canonicalLiquidityToken = '0x93E66202A11B1772E55407B32B44e5Cd8eda7f22'
  const [matrixResult, networksResult, poolsResult, truthContext] = await Promise.all([
    fetchPublicJson<RouteMatrixResponse>('/token-aggregation/api/v1/routes/matrix?includeNonLive=true').catch(() => null),
    fetchPublicJson<{ networks?: ExplorerNetwork[] }>('/token-aggregation/api/v1/networks').catch(() => null),
    fetchPublicJson<{ pools?: MissionControlLiquidityPool[] }>(
      `/token-aggregation/api/v1/tokens/${canonicalLiquidityToken}/pools`,
    ).catch(() => null),
    fetchExplorerTruthContext(),
  ])

  return {
    props: {
      initialRouteMatrix: matrixResult,
      initialNetworks: networksResult?.networks || [],
      initialPools: poolsResult?.pools || [],
      initialStats: truthContext.initialStats,
      initialBridgeStatus: truthContext.initialBridgeStatus,
    },
    revalidate: 60,
  }
}
