import { useRouter } from 'next/router'
import Head from 'next/head'
import { useEffect, useState } from 'react'
import { getPurchaseById } from '@/lib/purchaseQueries'
import type { Purchase } from '@/types/nostr'
import { PageHeader } from '@/components/PageHeader'
import { Footer } from '@/components/Footer'
import { t } from '@/lib/i18n'

export default function PurchasePage(): React.ReactElement | null {
  const router = useRouter()
  const { id } = router.query
  const purchaseId = typeof id === 'string' ? id : ''
  const [purchase, setPurchase] = useState<Purchase | null>(null)
  const [loading, setLoading] = useState(true)
  const [error, setError] = useState<string | null>(null)

  useEffect(() => {
    if (!purchaseId) {
      return
    }
    void loadPurchase({ purchaseId, setPurchase, setLoading, setError })
  }, [purchaseId])

  if (!purchaseId) {
    return null
  }

  return (
    <>
      <Head>
        <title>Paiement - zapwall.fr</title>
      </Head>
      <main className="min-h-screen bg-cyber-darker">
        <PageHeader />
        <div className="w-full px-4 py-8">
          {loading && <p className="text-sm text-cyber-accent">{t('common.loading')}</p>}
          {error && <p className="text-sm text-red-400">{error}</p>}
          {purchase && (
            <div className="bg-cyber-darker border border-neon-cyan/30 rounded-lg p-6 space-y-4">
              <h1 className="text-3xl font-bold text-neon-cyan">Paiement d&apos;article</h1>
              <PurchaseDetails purchase={purchase} />
            </div>
          )}
        </div>
        <Footer />
      </main>
    </>
  )
}

async function loadPurchase(params: {
  purchaseId: string
  setPurchase: (value: Purchase | null) => void
  setLoading: (value: boolean) => void
  setError: (value: string | null) => void
}): Promise<void> {
  params.setLoading(true)
  params.setError(null)
  try {
    const p = await getPurchaseById(params.purchaseId)
    if (!p) {
      params.setError('Paiement introuvable')
      return
    }
    params.setPurchase(p)
  } catch (e) {
    params.setError(e instanceof Error ? e.message : 'Erreur lors du chargement du paiement')
  } finally {
    params.setLoading(false)
  }
}

function PurchaseDetails(params: { purchase: Purchase }): React.ReactElement {
  const {purchase} = params
  return (
    <div className="space-y-2">
      <p className="text-cyber-accent">
        <span className="font-semibold">Montant :</span> {purchase.amount} sats
      </p>
      <p className="text-cyber-accent">
        <span className="font-semibold">Article ID :</span> {purchase.articleId}
      </p>
      <p className="text-cyber-accent">
        <span className="font-semibold">Auteur :</span> {purchase.authorPubkey.substring(0, 16)}...
      </p>
      <p className="text-cyber-accent">
        <span className="font-semibold">Payeur :</span> {purchase.payerPubkey.substring(0, 16)}...
      </p>
      <p className="text-cyber-accent">
        <span className="font-semibold">Hash de paiement :</span> {purchase.paymentHash}
      </p>
      <p className="text-cyber-accent">
        <span className="font-semibold">Date :</span> {new Date(purchase.createdAt * 1000).toLocaleString()}
      </p>
    </div>
  )
}
