'use client';

import { Reminder } from '@/types/reminder';
import { getReminderTypeLabel } from '@/types/reminder';
import { PencilIcon, TrashIcon, BellIcon, SparklesIcon } from '@heroicons/react/24/outline';
import { formatDate, formatTime } from '@/lib/utils';

interface ReminderListProps {
    reminders: Reminder[];
    packageName: string;
    onEdit: (reminder: Reminder) => void;
    onDelete: (reminder: Reminder) => void;
    isLoading: boolean;
}

export default function ReminderList({ reminders, packageName, onEdit, onDelete, isLoading }: ReminderListProps) {
    if (isLoading) {
        return (
            <div className="flex justify-center items-center py-16">
                <div className="h-10 w-10 border-4 border-indigo-200 dark:border-indigo-800 border-t-indigo-600 rounded-full animate-spin" />
            </div>
        );
    }

    if (!reminders || reminders.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">
                    <BellIcon className="h-6 w-6 text-gray-400" />
                </div>
                <p className="text-gray-500 dark:text-gray-400 text-sm">هیچ یادآوری برای پکیج {packageName} یافت نشد</p>
            </div>
        );
    }

    const sortedReminders = [...reminders].sort((a, b) =>
        new Date(a.time).getTime() - new Date(b.time).getTime()
    );

    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">
                <h3 className="text-base font-semibold text-gray-900 dark:text-gray-100">
                    یادآوری‌های پکیج {packageName}
                </h3>
            </div>
            <ul className="divide-y divide-gray-200 dark:divide-gray-800">
                {sortedReminders.map((reminder) => (
                    <li key={reminder.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">
                            <div className="flex-1 min-w-0">
                                <div className="flex items-center gap-2">
                                    <div className={`h-8 w-8 rounded-lg flex items-center justify-center flex-shrink-0 ${
                                        reminder.type === 'motivate'
                                            ? 'bg-yellow-100 dark:bg-yellow-500/10'
                                            : 'bg-indigo-100 dark:bg-indigo-500/10'
                                    }`}>
                                        {reminder.type === 'motivate' ? (
                                            <SparklesIcon className="h-4 w-4 text-yellow-600 dark:text-yellow-400" />
                                        ) : (
                                            <BellIcon 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">
                                        {reminder.title}
                                    </p>
                                    <span className="px-2 py-0.5 text-xs font-semibold rounded-lg bg-blue-100 text-blue-800 dark:bg-blue-900/30 dark:text-blue-300">
                                        {getReminderTypeLabel(reminder.type)}
                                    </span>
                                </div>

                                {reminder.description && (
                                    <p className="mt-2 text-sm text-gray-600 dark:text-gray-400 line-clamp-2">
                                        {reminder.description}
                                    </p>
                                )}

                                <div className="mt-2 flex flex-wrap gap-x-5 gap-y-1 text-xs text-gray-500 dark:text-gray-400">
                                    <span>زمان: {formatDate(reminder.time)} {formatTime(reminder.time)}</span>
                                    <span>تکرار: {reminder.repeatable ? 'فعال' : 'غیرفعال'}</span>
                                    {reminder.repeatable && reminder.repeat_days && (
                                        <span>هر {reminder.repeat_days} روز</span>
                                    )}
                                </div>
                                <p className="mt-2 text-xs text-gray-400 dark:text-gray-500">
                                    {formatDate(reminder.updated_at)}
                                </p>
                            </div>

                            <div className="flex items-center gap-1 flex-shrink-0">
                                <button
                                    onClick={() => onEdit(reminder)}
                                    className="p-2 rounded-lg text-indigo-600 dark:text-indigo-400 hover:bg-indigo-50 dark:hover:bg-indigo-500/10 transition-colors"
                                >
                                    <PencilIcon className="h-4 w-4" />
                                </button>
                                <button
                                    onClick={() => onDelete(reminder)}
                                    className="p-2 rounded-lg text-red-600 dark:text-red-400 hover:bg-red-50 dark:hover:bg-red-500/10 transition-colors"
                                >
                                    <TrashIcon className="h-4 w-4" />
                                </button>
                            </div>
                        </div>
                    </li>
                ))}
            </ul>
        </div>
    );
}
