'use client';

import { useState } from 'react';
import { User, UpdateUserProfileData } from '@/types/user';
import { XMarkIcon } from '@heroicons/react/24/outline';

interface UserEditFormProps {
    user: User;
    onSubmit: (data: UpdateUserProfileData) => void;
    onCancel: () => void;
    isLoading: boolean;
}

export default function UserEditForm({ user, onSubmit, onCancel, isLoading }: UserEditFormProps) {
    const [formData, setFormData] = useState<UpdateUserProfileData>({
        first_name: user.first_name || '',
        last_name: user.last_name || '',
        email: user.email || '',
        mobile: user.mobile || '',
        avatar: null,
    });

    const handleSubmit = (e: React.FormEvent) => {
        e.preventDefault();
        const payload: UpdateUserProfileData = {};
        if (formData.first_name !== (user.first_name || '')) payload.first_name = formData.first_name;
        if (formData.last_name !== (user.last_name || '')) payload.last_name = formData.last_name;
        if (formData.email !== (user.email || '')) payload.email = formData.email;
        if (formData.mobile !== (user.mobile || '')) payload.mobile = formData.mobile;
        if (formData.avatar) payload.avatar = formData.avatar;
        onSubmit(payload);
    };

    const inputClass = 'block w-full px-4 py-2.5 text-sm text-gray-900 dark:text-gray-100 border border-gray-200 dark:border-gray-700 rounded-xl bg-gray-50 dark:bg-gray-800/60 placeholder-gray-400 dark:placeholder-gray-500 focus:ring-2 focus:ring-indigo-500/20 focus:border-indigo-500 dark:focus:border-indigo-400 transition-colors';
    const labelClass = 'block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1.5';

    return (
        <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/40 dark:bg-black/60 p-4">
            <div className="bg-white dark:bg-gray-900 rounded-2xl shadow-xl ring-1 ring-gray-200 dark:ring-gray-800 w-full max-w-lg" dir="rtl">
                <div className="flex items-center justify-between px-6 py-4 border-b border-gray-200 dark:border-gray-800">
                    <h3 className="text-lg font-semibold text-gray-900 dark:text-gray-100">ویرایش اطلاعات کاربر</h3>
                    <button onClick={onCancel} className="p-2 rounded-lg text-gray-400 hover:text-gray-600 dark:text-gray-500 dark:hover:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-800 transition-colors" type="button">
                        <XMarkIcon className="h-5 w-5" />
                    </button>
                </div>

                <form onSubmit={handleSubmit} className="px-6 py-5 space-y-4">
                    <div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
                        <div>
                            <label className={labelClass}>نام</label>
                            <input type="text" className={inputClass} value={formData.first_name || ''} onChange={(e) => setFormData({ ...formData, first_name: e.target.value })} />
                        </div>
                        <div>
                            <label className={labelClass}>نام خانوادگی</label>
                            <input type="text" className={inputClass} value={formData.last_name || ''} onChange={(e) => setFormData({ ...formData, last_name: e.target.value })} />
                        </div>
                    </div>

                    <div>
                        <label className={labelClass}>ایمیل</label>
                        <input type="email" dir="ltr" className={inputClass} value={formData.email || ''} onChange={(e) => setFormData({ ...formData, email: e.target.value })} />
                    </div>

                    <div>
                        <label className={labelClass}>شماره موبایل</label>
                        <input type="text" dir="ltr" placeholder="09xxxxxxxxx" className={inputClass} value={formData.mobile || ''} onChange={(e) => setFormData({ ...formData, mobile: e.target.value })} />
                    </div>

                    <div>
                        <label className={labelClass}>آواتار</label>
                        <input
                            type="file"
                            accept="image/*"
                            className="block w-full text-sm text-gray-600 dark:text-gray-400 file:ml-0 file:py-2 file:px-4 file:rounded-xl file:border-0 file:text-sm file:font-medium file:bg-indigo-50 file:text-indigo-700 hover:file:bg-indigo-100 dark:file:bg-indigo-500/10 dark:file:text-indigo-400 dark:hover:file:bg-indigo-500/15 transition-colors"
                            onChange={(e) => setFormData({ ...formData, avatar: e.target.files?.[0] || null })}
                        />
                    </div>

                    <div className="flex justify-end gap-3 pt-3 border-t border-gray-100 dark:border-gray-800">
                        <button type="button" onClick={onCancel} disabled={isLoading} className="px-5 py-2.5 rounded-xl border border-gray-200 dark:border-gray-700 text-sm font-medium text-gray-700 dark:text-gray-200 bg-white dark:bg-gray-800 hover:bg-gray-50 dark:hover:bg-gray-700 disabled:opacity-50 transition-colors">
                            انصراف
                        </button>
                        <button type="submit" disabled={isLoading} className="px-5 py-2.5 rounded-xl bg-indigo-600 text-white text-sm font-medium hover:bg-indigo-700 focus:ring-2 focus:ring-indigo-500/30 disabled:opacity-50 transition-colors shadow-sm shadow-indigo-500/20">
                            {isLoading ? 'در حال ذخیره...' : 'ذخیره تغییرات'}
                        </button>
                    </div>
                </form>
            </div>
        </div>
    );
}
