import type { GetStaticProps } from 'next'
import HoLiquidityPage, { type HoLiquiditySnapshot } from '@/components/explorer/HoLiquidityPage'

type PageProps = {
  snapshot: HoLiquiditySnapshot | null
  loadError: string | null
}

export default function HoLiquidityRoute(props: PageProps) {
  return <HoLiquidityPage {...props} />
}

export const getStaticProps: GetStaticProps<PageProps> = async () => {
  const base = process.env.EXPLORER_PUBLIC_BASE_URL || 'https://explorer.d-bis.org'
  try {
    const res = await fetch(`${base}/token-aggregation/api/v1/omnl/ho-liquidity-snapshot`, {
      headers: { Accept: 'application/json' },
    })
    if (!res.ok) {
      return {
        props: { snapshot: null, loadError: `ho-liquidity-snapshot HTTP ${res.status}` },
        revalidate: 300,
      }
    }
    const snapshot = (await res.json()) as HoLiquiditySnapshot
    return { props: { snapshot, loadError: null }, revalidate: 300 }
  } catch (error) {
    return {
      props: {
        snapshot: null,
        loadError: error instanceof Error ? error.message : 'ho-liquidity-snapshot unavailable',
      },
      revalidate: 300,
    }
  }
}
