'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();

        // Only send changed fields so we never overwrite values with empty ones
        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-3 py-2 border text-slate-900 dark:text-gray-100 border-gray-300 dark:border-gray-600 rounded-md leading-5 bg-white dark:bg-gray-800 placeholder-gray-400 dark:placeholder-gray-500 focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm transition-colors';

    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-xl 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-medium text-gray-900 dark:text-gray-100">ویرایش اطلاعات کاربر</h3>
                    <button
                        onClick={onCancel}
                        className="text-gray-400 hover:text-gray-600 dark:text-gray-500 dark:hover:text-gray-300 transition-colors"
                        type="button"
                    >
                        <XMarkIcon className="h-5 w-5" />
                    </button>
                </div>

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

                    <div>
                        <label className="block text-sm font-medium text-gray-700 mb-1">ایمیل</label>
                        <input
                            type="email"
                            dir="ltr"
                            className={inputClass}
                            value={formData.email || ''}
                            onChange={(e) => setFormData({ ...formData, email: e.target.value })}
                        />
                    </div>

                    <div>
                        <label className="block text-sm font-medium text-gray-700 mb-1">شماره موبایل</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="block text-sm font-medium text-gray-700 mb-1">آواتار</label>
                        <input
                            type="file"
                            accept="image/*"
                            className="block w-full text-sm text-gray-700 dark:text-gray-300 file:ml-4 file:py-2 file:px-4 file:rounded-md 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-950/40 dark:file:text-indigo-300 dark:hover:file:bg-indigo-900/40"
                            onChange={(e) =>
                                setFormData({ ...formData, avatar: e.target.files?.[0] || null })
                            }
                        />
                    </div>

                    <div className="flex justify-end gap-2 pt-2">
                        <button
                            type="button"
                            onClick={onCancel}
                            disabled={isLoading}
                            className="px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-md 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-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 dark:focus:ring-offset-gray-900 disabled:opacity-50 transition-colors"
                        >
                            {isLoading ? 'در حال ذخیره...' : 'ذخیره تغییرات'}
                        </button>
                    </div>
                </form>
            </div>
        </div>
    );
}
