'use client';

import { useState } from 'react';
import { updateSettings } from '../../actions';
import { Save, CheckCircle2, AlertCircle } from 'lucide-react';

export default function SettingsForm({ initialData }: { initialData: any }) {
    const [status, setStatus] = useState<'idle' | 'loading' | 'success' | 'error'>('idle');

    async function handleSubmit(formData: FormData) {
        setStatus('loading');
        const res = await updateSettings(formData);
        if (res.error) setStatus('error');
        else setStatus('success');
        
        setTimeout(() => setStatus('idle'), 3000);
    }

    return (
        <form action={handleSubmit} className="bg-zinc-950 border border-white/10 rounded-3xl p-8 space-y-6">
            <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
                <div className="space-y-2">
                    <label className="text-[10px] font-bold uppercase tracking-widest text-white/40 ml-1">Site Adı</label>
                    <input type="text" name="site_name" defaultValue={initialData?.site_name} className="w-full bg-white/5 border border-white/10 rounded-xl px-4 py-3 text-white focus:border-[#1e9a83] outline-none" />
                </div>
                <div className="space-y-2">
                    <label className="text-[10px] font-bold uppercase tracking-widest text-white/40 ml-1">İletişim E-posta</label>
                    <input type="email" name="contact_email" defaultValue={initialData?.contact_email} className="w-full bg-white/5 border border-white/10 rounded-xl px-4 py-3 text-white focus:border-[#1e9a83] outline-none" />
                </div>
                <div className="space-y-2">
                    <label className="text-[10px] font-bold uppercase tracking-widest text-white/40 ml-1">İletişim Telefon</label>
                    <input type="text" name="contact_phone" defaultValue={initialData?.contact_phone} className="w-full bg-white/5 border border-white/10 rounded-xl px-4 py-3 text-white focus:border-[#1e9a83] outline-none" />
                </div>
                <div className="space-y-2">
                    <label className="text-[10px] font-bold uppercase tracking-widest text-white/40 ml-1">Rozet Metni</label>
                    <input type="text" name="status_badge_text" defaultValue={initialData?.status_badge_text} className="w-full bg-white/5 border border-white/10 rounded-xl px-4 py-3 text-white focus:border-[#1e9a83] outline-none" />
                </div>
            </div>

            <div className="space-y-2">
                <label className="text-[10px] font-bold uppercase tracking-widest text-white/40 ml-1">Site Açıklaması (SEO)</label>
                <textarea name="site_description" rows={3} defaultValue={initialData?.site_description} className="w-full bg-white/5 border border-white/10 rounded-xl px-4 py-3 text-white focus:border-[#1e9a83] outline-none resize-none" />
            </div>

            <div className="space-y-2">
                <label className="text-[10px] font-bold uppercase tracking-widest text-white/40 ml-1">Ofis Adresi</label>
                <textarea name="office_address" rows={2} defaultValue={initialData?.office_address} className="w-full bg-white/5 border border-white/10 rounded-xl px-4 py-3 text-white focus:border-[#1e9a83] outline-none resize-none" />
            </div>

            <h3 className="text-sm font-bold uppercase tracking-widest text-white/60 mt-8 mb-4 border-b border-white/10 pb-2">Sosyal Medya Linkleri</h3>
            
            <div className="grid grid-cols-1 md:grid-cols-3 gap-6">
                <div className="space-y-2">
                    <label className="text-[10px] font-bold uppercase tracking-widest text-white/40 ml-1">Instagram URL</label>
                    <input type="url" name="instagram_url" defaultValue={initialData?.instagram_url} className="w-full bg-white/5 border border-white/10 rounded-xl px-4 py-3 text-white focus:border-[#1e9a83] outline-none" />
                </div>
                <div className="space-y-2">
                    <label className="text-[10px] font-bold uppercase tracking-widest text-white/40 ml-1">Twitter URL</label>
                    <input type="url" name="twitter_url" defaultValue={initialData?.twitter_url} className="w-full bg-white/5 border border-white/10 rounded-xl px-4 py-3 text-white focus:border-[#1e9a83] outline-none" />
                </div>
                <div className="space-y-2">
                    <label className="text-[10px] font-bold uppercase tracking-widest text-white/40 ml-1">LinkedIn URL</label>
                    <input type="url" name="linkedin_url" defaultValue={initialData?.linkedin_url} className="w-full bg-white/5 border border-white/10 rounded-xl px-4 py-3 text-white focus:border-[#1e9a83] outline-none" />
                </div>
            </div>

            <div className="pt-6">
                <button type="submit" disabled={status === 'loading'} className="bg-[#1e9a83] hover:bg-[#157a67] text-white font-bold py-3 px-8 rounded-xl transition-colors flex items-center gap-2 disabled:opacity-50">
                    <Save className="w-5 h-5" />
                    {status === 'loading' ? 'Kaydediliyor...' : 'Ayarları Kaydet'}
                </button>
            </div>

            {status === 'success' && (
                <div className="flex items-center gap-2 text-green-500 text-sm font-bold bg-green-500/10 p-4 rounded-xl mt-4">
                    <CheckCircle2 className="w-5 h-5" /> Ayarlar başarıyla güncellendi!
                </div>
            )}
            {status === 'error' && (
                <div className="flex items-center gap-2 text-red-500 text-sm font-bold bg-red-500/10 p-4 rounded-xl mt-4">
                    <AlertCircle className="w-5 h-5" /> Güncelleme sırasında bir hata oluştu.
                </div>
            )}
        </form>
    );
}
