/**
 * EmailInboxWidget - Gmail/Outlook Inbox Monitor
 */

import React from 'react';
import { Mail, Inbox, Star, Archive } from 'lucide-react';
import { useLiveData } from '@/hooks/useLiveData';
import { useWidgetCommunication } from '@/contexts/WidgetContext';
import { cn } from '@/lib/utils';

export default function EmailInboxWidget({ widgetId }: { widgetId: string }) {
    const { data, connected } = useLiveData({
        widgetId,
        widgetType: 'email',
        requiredSources: ['gmail', 'outlook'],
        autoConnect: true,
        pollInterval: 60000,
    });

    const { broadcastEvent } = useWidgetCommunication(widgetId);

    const unreadCount = data?.unreadCount || 0;
    const recentEmails = data?.emails || [];

    return (
        <div className="h-full flex flex-col bg-gradient-to-br from-pink-500/10 to-rose-500/10 backdrop-blur-sm border border-pink-500/30 rounded-lg overflow-hidden">
            <div className="p-4 border-b border-pink-500/20">
                <div className="flex items-center justify-between">
                    <div className="flex items-center gap-2">
                        <Mail className="w-5 h-5 text-pink-400" />
                        <span className="font-display text-sm uppercase tracking-wider text-pink-400">Inbox</span>
                    </div>
                    <div className={cn(
                        "px-3 py-1 rounded-full text-xs font-semibold",
                        unreadCount > 0 ? "bg-pink-500 text-white" : "bg-pink-500/20 text-pink-400"
                    )}>
                        {unreadCount}
                    </div>
                </div>
            </div>

            <div className="flex-1 overflow-y-auto p-3 space-y-2">
                {recentEmails.slice(0, 5).map((email: any) => (
                    <div key={email.id} className="p-3 bg-pink-500/10 border border-pink-500/30 rounded-lg hover:bg-pink-500/20 transition-all cursor-pointer">
                        <div className="flex items-start justify-between gap-2">
                            <div className="flex-1">
                                <div className="text-sm font-medium text-white mb-1">{email.from}</div>
                                <div className="text-xs text-pink-300">{email.subject}</div>
                            </div>
                            {email.starred && <Star className="w-4 h-4 text-yellow-400" />}
                        </div>
                    </div>
                ))}
            </div>

            <div className="p-3 border-t border-pink-500/20 bg-black/20">
                <button className="w-full py-2 bg-pink-500 hover:bg-pink-600 text-white rounded text-sm transition-colors">
                    Open Inbox
                </button>
            </div>
        </div>
    );
}
