'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-12">
                <div className="animate-spin rounded-full h-12 w-12 border-b-2 border-indigo-600"></div>
            </div>
        );
    }

    if (!reminders || reminders.length === 0) {
        return (
            <div className="text-center py-12 bg-white rounded-lg shadow">
                <p className="text-gray-500">هیچ یادآوری برای این پکیج یافت نشد</p>
            </div>
        );
    }

    // Sort reminders by time (most recent first)
    const sortedReminders = [...reminders].sort((a, b) => 
        new Date(a.time).getTime() - new Date(b.time).getTime()
    );

    return (
        <div className="bg-white shadow overflow-hidden sm:rounded-lg">
            <div className="px-4 py-5 sm:px-6">
                <h3 className="text-lg leading-6 font-medium text-gray-900">
                    یادآوری‌های پکیج {packageName}
                </h3>
            </div>
            <ul className="divide-y divide-gray-200">
                {sortedReminders.map((reminder) => (
                    <li key={reminder.id} className="px-6 py-4 hover:bg-gray-50">
                        <div className="flex items-center justify-between">
                            <div className="flex-1 min-w-0">
                                <div className="flex items-center justify-between">
                                    <div className="flex items-center space-x-2 space-x-reverse">
                                        {reminder.type === 'motivate' ? (
                                            <SparklesIcon className="h-5 w-5 text-yellow-500" />
                                        ) : (
                                            <BellIcon className="h-5 w-5 text-indigo-500" />
                                        )}
                                        <p className="text-sm font-medium text-gray-900">
                                            {reminder.title}
                                        </p>
                                    </div>
                                    <span className="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-blue-100 text-blue-800">
                                        {getReminderTypeLabel(reminder.type)}
                                    </span>
                                </div>
                                
                                {reminder.description && (
                                    <p className="mt-1 text-sm text-gray-600">
                                        {reminder.description}
                                    </p>
                                )}
                                
                                <div className="mt-2 grid grid-cols-1 gap-2 sm:grid-cols-3">
                                    <div className="text-xs text-gray-500">
                                        <span className="font-medium">زمان:</span> {formatDate(reminder.time)} {formatTime(reminder.time)}
                                    </div>
                                    
                                    <div className="text-xs text-gray-500">
                                        <span className="font-medium">تکرار:</span> {reminder.repeatable ? 'فعال' : 'غیرفعال'}
                                    </div>
                                    
                                    {reminder.repeatable && reminder.repeat_days && (
                                        <div className="text-xs text-gray-500">
                                            <span className="font-medium">فاصله تکرار:</span> هر {reminder.repeat_days} روز
                                        </div>
                                    )}
                                </div>
                                
                                <div className="mt-2 text-xs text-gray-400">
                                    آخرین به‌روزرسانی: {formatDate(reminder.updated_at)}
                                </div>
                            </div>
                            
                            <div className="mr-4 flex-shrink-0 flex space-x-2 space-x-reverse">
                                <button
                                    onClick={() => onEdit(reminder)}
                                    className="text-indigo-600 hover:text-indigo-900 p-2 rounded-full hover:bg-indigo-50"
                                >
                                    <PencilIcon className="h-5 w-5" />
                                </button>
                                <button
                                    onClick={() => onDelete(reminder)}
                                    className="text-red-600 hover:text-red-900 p-2 rounded-full hover:bg-red-50"
                                >
                                    <TrashIcon className="h-5 w-5" />
                                </button>
                            </div>
                        </div>
                    </li>
                ))}
            </ul>
        </div>
    );
}