'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-16 bg-white dark:bg-gray-900 rounded-2xl ring-1 ring-gray-200 dark:ring-gray-800 shadow-sm">
                <div className="mx-auto h-12 w-12 rounded-xl bg-gray-100 dark:bg-gray-800 flex items-center justify-center mb-3">
                    <GiftIcon className="h-6 w-6 text-gray-400" />
                </div>
                <p className="text-gray-500 dark:text-gray-400 text-sm">
                    در حال حاضر هیچ کاربری واجد شرایط نیست
                </p>
                <p className="text-xs text-gray-400 dark:text-gray-500 mt-1">
                    کاربران پس از {subscriptionTarget} دعوت موفق در این لیست نمایش داده می‌شوند
                </p>
            </div>
        );
    }

    return (
        <div className="bg-white dark:bg-gray-900 ring-1 ring-gray-200 dark:ring-gray-800 rounded-2xl overflow-hidden shadow-sm">
            <div className="px-5 py-4 border-b border-gray-200 dark:border-gray-800 flex items-center justify-between">
                <h3 className="text-base font-semibold text-gray-900 dark:text-gray-100">
                    کاربران واجد شرایط
                </h3>
                <span className="px-3 py-1 text-xs font-semibold rounded-lg bg-indigo-100 text-indigo-700 dark:bg-indigo-500/15 dark:text-indigo-400">
                    هدف: {subscriptionTarget} دعوت
                </span>
            </div>
            <ul className="divide-y divide-gray-200 dark:divide-gray-800">
                {users.map((user) => (
                    <li key={user.id} className="px-5 py-4 hover:bg-gray-50 dark:hover:bg-gray-800/40 transition-colors">
                        <div className="flex items-start justify-between gap-4 flex-wrap">
                            <div className="flex-1 min-w-0">
                                <div className="flex items-center gap-2 flex-wrap">
                                    <div className="h-8 w-8 rounded-lg bg-indigo-100 dark:bg-indigo-500/10 flex items-center justify-center flex-shrink-0">
                                        <UserIcon className="h-4 w-4 text-indigo-600 dark:text-indigo-400" />
                                    </div>
                                    <p className="text-sm font-semibold text-gray-900 dark:text-gray-100">
                                        {user.name || 'بدون نام'}
                                    </p>
                                    <span className="inline-flex items-center gap-1 px-2 py-0.5 text-xs font-semibold rounded-lg bg-green-50 text-green-700 dark:bg-green-500/10 dark:text-green-400">
                                        <span className="h-1.5 w-1.5 rounded-full bg-green-500" />
                                        {user.successful_invites} دعوت موفق
                                    </span>
                                </div>
                                <div className="mt-2 flex flex-wrap gap-x-5 gap-y-1 text-xs text-gray-500 dark:text-gray-400">
                                    {user.email && (
                                        <span className="flex items-center gap-1" dir="ltr">
                                            <EnvelopeIcon className="h-3.5 w-3.5" />
                                            {user.email}
                                        </span>
                                    )}
                                    {user.mobile && (
                                        <span className="flex items-center gap-1" dir="ltr">
                                            <PhoneIcon className="h-3.5 w-3.5" />
                                            {user.mobile}
                                        </span>
                                    )}
                                    {user.referral_code && (
                                        <span className="flex items-center gap-1">
                                            <TicketIcon className="h-3.5 w-3.5" />
                                            {user.referral_code}
                                        </span>
                                    )}
                                </div>
                            </div>
                            <button
                                onClick={() => onFulfill(user)}
                                disabled={isLoading}
                                className="flex-shrink-0 inline-flex items-center gap-2 px-4 py-2 bg-green-600 text-white rounded-xl text-sm font-medium 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 shadow-sm shadow-green-500/20"
                            >
                                <GiftIcon className="h-4 w-4" />
                                {fulfillingId === user.id ? 'در حال اعطا...' : 'اعطای اشتراک'}
                            </button>
                        </div>
                    </li>
                ))}
            </ul>
        </div>
    );
}
