'use client'

import { useState } from 'react'
import { useRouter } from 'next/navigation'
import { Loader2 } from 'lucide-react'
import { saveWidgetPartnerAction } from '@/app/actions'

export default function WidgetPartnerForm({ initialData }: { initialData?: any }) {
  const router = useRouter()
  const [isPending, setIsPending] = useState(false)
  const [error, setError] = useState<string | null>(null)

  async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
    e.preventDefault()
    setIsPending(true)
    setError(null)
    
    const formData = new FormData(e.currentTarget)
    if (initialData?.id) formData.append('id', initialData.id)
    
    try {
      const res = await saveWidgetPartnerAction(formData)
      if (res?.error) {
        setError(res.error)
      } else {
        router.push('/tr/admin/widget-partners')
        router.refresh()
      }
    } catch (err) {
      setError('Bir hata oluştu')
    } finally {
      setIsPending(false)
    }
  }

  return (
    <form onSubmit={handleSubmit} className="bg-paper p-8 rounded-3xl border border-pine/10 shadow-sm space-y-6">
      {error && (
        <div className="bg-red-50 text-red-500 p-4 rounded-xl text-sm">
          {error}
        </div>
      )}
      
      <div>
        <label className="block text-xs font-mono uppercase tracking-wider text-shutter mb-2">Partner Adı *</label>
        <input 
          type="text" 
          name="name"
          defaultValue={initialData?.name}
          required
          className="w-full bg-stone border border-pine/10 rounded-xl px-4 py-3 text-sm focus:ring-2 focus:ring-turquoise/20 outline-none transition"
        />
      </div>

      <div>
        <label className="block text-xs font-mono uppercase tracking-wider text-shutter mb-2">Logo URL *</label>
        <input 
          type="text" 
          name="logoUrl"
          defaultValue={initialData?.logoUrl}
          required
          className="w-full bg-stone border border-pine/10 rounded-xl px-4 py-3 text-sm focus:ring-2 focus:ring-turquoise/20 outline-none transition"
        />
      </div>

      <div>
        <label className="block text-xs font-mono uppercase tracking-wider text-shutter mb-2">Web Sitesi *</label>
        <input 
          type="text" 
          name="websiteUrl"
          defaultValue={initialData?.websiteUrl}
          required
          className="w-full bg-stone border border-pine/10 rounded-xl px-4 py-3 text-sm focus:ring-2 focus:ring-turquoise/20 outline-none transition"
        />
      </div>

      <div>
        <label className="block text-xs font-mono uppercase tracking-wider text-shutter mb-2">Widget Tipi *</label>
        <select 
          name="widgetType"
          defaultValue={initialData?.widgetType || 'listing'}
          required
          className="w-full bg-stone border border-pine/10 rounded-xl px-4 py-3 text-sm focus:ring-2 focus:ring-turquoise/20 outline-none transition"
        >
          <option value="listing">Listing</option>
          <option value="collection">Collection</option>
          <option value="custom">Custom</option>
        </select>
      </div>

      <div className="pt-6 border-t border-pine/10 flex justify-end gap-3">
        <button
          type="button"
          onClick={() => router.back()}
          className="px-6 py-3 rounded-xl border border-pine/10 text-pine font-bold text-sm hover:bg-stone transition"
        >
          İptal
        </button>
        <button
          type="submit"
          disabled={isPending}
          className="px-6 py-3 rounded-xl bg-turquoise text-white font-bold text-sm hover:bg-turquoise/90 transition flex items-center gap-2"
        >
          {isPending && <Loader2 className="w-4 h-4 animate-spin" />}
          Kaydet
        </button>
      </div>
    </form>
  )
}
