'use client'

import Head from 'next/head'
import { useRouter } from 'next/router'

const BASE_TITLE = 'DBIS Explorer'

function resolveDocumentTitle(pathname: string, query: Record<string, string | string[] | undefined>): string {
  if (pathname === '/') return `${BASE_TITLE} — Chain 138`
  if (pathname === '/search') return `Search — ${BASE_TITLE}`
  if (pathname === '/wallet') return `Wallet Tools — ${BASE_TITLE}`
  if (pathname === '/protocols') return `Official Protocol Contracts — ${BASE_TITLE}`
  if (pathname.startsWith('/protocols/')) {
    const id = typeof query.id === 'string' ? query.id : pathname.split('/').pop() || 'Protocol'
    return `${id} — Protocols — ${BASE_TITLE}`
  }
  if (pathname === '/pools') return `Pool Registry — ${BASE_TITLE}`
  if (pathname.startsWith('/pools/')) return `Pool Detail — ${BASE_TITLE}`
  if (pathname === '/liquidity') return `Liquidity — ${BASE_TITLE}`
  if (pathname === '/operations') return `Operations — ${BASE_TITLE}`
  if (pathname === '/docs') return `Documentation — ${BASE_TITLE}`
  if (pathname === '/tokens') return `Tokens — ${BASE_TITLE}`
  if (pathname.startsWith('/tokens/')) return `Token — ${BASE_TITLE}`
  if (pathname === '/addresses') return `Addresses — ${BASE_TITLE}`
  if (pathname.startsWith('/addresses/')) return `Address — ${BASE_TITLE}`
  if (pathname === '/blocks') return `Blocks — ${BASE_TITLE}`
  if (pathname === '/transactions') return `Transactions — ${BASE_TITLE}`
  if (pathname.startsWith('/transactions/')) return `Transaction — ${BASE_TITLE}`
  if (pathname === '/bridge') return `Bridge — ${BASE_TITLE}`
  if (pathname === '/routes') return `Routes — ${BASE_TITLE}`
  if (pathname === '/analytics') return `Analytics — ${BASE_TITLE}`
  if (pathname === '/operator') return `Operator — ${BASE_TITLE}`
  if (pathname === '/system') return `System — ${BASE_TITLE}`
  if (pathname === '/weth') return `WETH — ${BASE_TITLE}`
  if (pathname === '/watchlist') return `Watchlist — ${BASE_TITLE}`
  if (pathname === '/access') return `Account Access — ${BASE_TITLE}`

  const segment = pathname.split('/').filter(Boolean)[0]
  if (segment) {
    const label = segment.charAt(0).toUpperCase() + segment.slice(1)
    return `${label} — ${BASE_TITLE}`
  }
  return BASE_TITLE
}

export default function ExplorerDocumentHead() {
  const router = useRouter()
  const pathname = router.pathname || '/'
  const title = resolveDocumentTitle(pathname, router.query)

  return (
    <Head>
      <title>{title}</title>
    </Head>
  )
}
