'use client';

import { ReferralRewardUser } from '@/types/referral';
import { GiftIcon, EnvelopeIcon, PhoneIcon, TicketIcon, UserIcon } from '@heroicons/react/24/outline';

interface ReferralListProps {
    users: ReferralRewardUser[];
    subscriptionTarget: number;
    onFulfill: (user: ReferralRewardUser) => void;
    isLoading: boolean;
    fulfillingId: number | null;
}

export default function ReferralList({
    users,
    subscriptionTarget,
    onFulfill,
    isLoading,
    fulfillingId
}: ReferralListProps) {
    if (users.length === 0) {
        return (
            <div className="text-center py-12 bg-white dark:bg-gray-900 rounded-xl shadow-sm ring-1 ring-gray-200 dark:ring-gray-800">
                <GiftIcon className="h-10 w-10 text-gray-300 dark:text-gray-600 mx-auto mb-3" />
                <p className="text-gray-500 dark:text-gray-400">
                    در حال حاضر هیچ کاربری واجد شرایط دریافت پاداش معرفی نیست.
                </p>
                <p className="text-sm text-gray-400 dark:text-gray-500 mt-1">
                    کاربران پس از {subscriptionTarget} دعوت موفق در این لیست نمایش داده می‌شوند.
                </p>
            </div>
        );
    }

    return (
        <div className="bg-white dark:bg-gray-900 shadow-sm ring-1 ring-gray-200 dark:ring-gray-800 overflow-hidden sm:rounded-xl">
            <div className="px-4 py-5 sm:px-6 flex items-center justify-between">
                <h3 className="text-lg leading-6 font-medium text-gray-900 dark:text-gray-100">
                    کاربران واجد شرایط پاداش
                </h3>
                <span className="px-3 py-1 text-xs font-medium rounded-full bg-indigo-100 text-indigo-800 dark:bg-indigo-900/30 dark:text-indigo-300">
                    هدف: {subscriptionTarget} دعوت موفق
                </span>
            </div>
            <ul className="divide-y divide-gray-200 dark:divide-gray-800 border-t border-gray-200 dark:border-gray-800">
                {users.map((user) => (
                    <li key={user.id} className="px-4 py-4 sm:px-6 hover:bg-gray-50 dark:hover:bg-gray-800/60 transition-colors">
                        <div className="flex items-center justify-between flex-wrap gap-3">
                            <div className="flex-1 min-w-0">
                                <div className="flex items-center gap-2">
                                    <UserIcon className="h-4 w-4 text-gray-400 dark:text-gray-500" />
                                    <p className="text-sm font-medium text-gray-900 dark:text-gray-100 truncate">
                                        {user.name || 'بدون نام'}
                                    </p>
                                    <span className="px-2 py-0.5 text-xs font-semibold rounded-full bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-300">
                                        {user.successful_invites} دعوت موفق
                                    </span>
                                </div>
                                <div className="mt-2 flex flex-wrap gap-x-6 gap-y-1 text-sm text-gray-500 dark:text-gray-400">
                                    <span className="flex items-center">
                                        <EnvelopeIcon className="h-4 w-4 ml-1 text-gray-400 dark:text-gray-500" />
                                        {user.email || 'ایمیل ثبت نشده'}
                                    </span>
                                    <span className="flex items-center">
                                        <PhoneIcon className="h-4 w-4 ml-1 text-gray-400 dark:text-gray-500" />
                                        {user.mobile || 'شماره ثبت نشده'}
                                    </span>
                                    {user.referral_code && (
                                        <span className="flex items-center">
                                            <TicketIcon className="h-4 w-4 ml-1 text-gray-400 dark:text-gray-500" />
                                            کد معرف: {user.referral_code}
                                        </span>
                                    )}
                                </div>
                            </div>
                            <button
                                onClick={() => onFulfill(user)}
                                disabled={isLoading}
                                className="inline-flex items-center px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-green-600 hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500 dark:focus:ring-offset-gray-900 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
                            >
                                <GiftIcon className="h-5 w-5 ml-2" />
                                {fulfillingId === user.id ? 'در حال اعطا...' : 'اعطای اشتراک رایگان'}
                            </button>
                        </div>
                    </li>
                ))}
            </ul>
        </div>
    );
}
