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

export default function SponsoringPage(): React.ReactElement | null {
  const router = useRouter()
  const { id } = router.query
  const sponsoringId = typeof id === 'string' ? id : ''
  const [sponsoring, setSponsoring] = useState<Sponsoring | null>(null)
  const [loading, setLoading] = useState(true)
  const [error, setError] = useState<string | null>(null)

  useEffect(() => {
    if (!sponsoringId) {
      return
    }
    void loadSponsoring({ sponsoringId, setSponsoring, setLoading, setError })
  }, [sponsoringId])

  if (!sponsoringId) {
    return null
  }

  return (
    <>
      <Head>
        <title>Sponsoring - 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>}
          {sponsoring && (
            <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">Sponsoring</h1>
              <SponsoringDetails sponsoring={sponsoring} />
            </div>
          )}
        </div>
        <Footer />
      </main>
    </>
  )
}

async function loadSponsoring(params: {
  sponsoringId: string
  setSponsoring: (value: Sponsoring | null) => void
  setLoading: (value: boolean) => void
  setError: (value: string | null) => void
}): Promise<void> {
  params.setLoading(true)
  params.setError(null)
  try {
    const s = await getSponsoringById(params.sponsoringId)
    if (!s) {
      params.setError('Sponsoring introuvable')
      return
    }
    params.setSponsoring(s)
  } catch (e) {
    params.setError(e instanceof Error ? e.message : 'Erreur lors du chargement du sponsoring')
  } finally {
    params.setLoading(false)
  }
}

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