'use client';
import { useEffect, useState } from 'react';
import { useParams } from 'next/navigation';
import { VehicleForm } from '../vehicle-form';
import { useLanguage } from '@/lib/i18n/language-context';

export default function EditVehiclePage() {
  const { id } = useParams();
  const [vehicle, setVehicle] = useState<any>(null);
  const [loading, setLoading] = useState(true);
  const { t } = useLanguage();

  useEffect(() => {
    fetch(`/api/vehicles/${id}`)
      .then((r: any) => r?.json?.())
      .then((d: any) => setVehicle(d ?? null))
      .catch(() => {})
      .finally(() => setLoading(false));
  }, [id]);

  if (loading) {
    return (
      <div className="flex items-center justify-center py-20">
        <div className="animate-spin w-8 h-8 border-4 border-red-600 border-t-transparent rounded-full" />
      </div>
    );
  }

  return (
    <div>
      <h1 className="font-display text-2xl font-bold tracking-tight mb-6">{t.admin.vehicles.editVehicle}</h1>
      {vehicle && <VehicleForm vehicle={vehicle} />}
    </div>
  );
}
