'use client'

import { useEffect, useState } from 'react'
import { useParams, useRouter } from 'next/navigation'
import { WearPartWithHistory } from '@/types'
import BikeDetail from '@/app/components/BikeDetail'

export default function BikeDetailPage() {
  const params = useParams()
  const router = useRouter()
  const [bike, setBike] = useState<any>(null)
  const [loading, setLoading] = useState(true)

  useEffect(() => {
    if (params.id) {
      fetchBike(params.id as string)
    }
  }, [params.id])

  const fetchBike = async (id: string) => {
    try {
      const response = await fetch(`/api/bikes/${id}`)
      if (response.ok) {
        const data = await response.json()
        setBike(data)
      } else if (response.status === 404) {
        router.push('/')
      }
    } catch (error) {
      console.error('Error fetching bike:', error)
    } finally {
      setLoading(false)
    }
  }

  if (loading) {
    return (
      <div className="min-h-screen flex items-center justify-center">
        <div className="text-lg">Lade...</div>
      </div>
    )
  }

  if (!bike) {
    return null
  }

  return <BikeDetail bike={bike} onUpdate={() => fetchBike(params.id as string)} />
}

