"use client";

import Link from "next/link";
import { ArrowLeft } from "lucide-react";
import { ConversationSummary } from "@/app/actions/conversationActions";

interface ConversationListProps {
    conversations: ConversationSummary[];
    activeApplicationId: string;
    onlineUsers?: Set<string>;
}

export function ConversationList({ conversations, activeApplicationId, onlineUsers = new Set() }: ConversationListProps) {
    return (
        <div className="w-[300px] shrink-0 border-r border-slate-100 overflow-y-auto hidden lg:flex flex-col bg-slate-50/50">
            <div className="p-4 border-b border-slate-100 sticky top-0 bg-slate-50/80 backdrop-blur-sm z-10 flex-shrink-0">
                <Link
                    href="/dashboard/messages"
                    className="flex items-center gap-2 text-sm font-semibold text-slate-500 hover:text-primary transition-colors"
                >
                    <ArrowLeft className="w-4 h-4" />
                    All Messages
                </Link>
            </div>
            <div className="flex-1 overflow-y-auto">
                {conversations.map((conv) => (
                    <Link
                        key={conv.applicationId}
                        href={`/dashboard/messages/${conv.applicationId}`}
                        className={`block px-4 py-3 border-b border-slate-100 transition-colors ${conv.applicationId === activeApplicationId
                            ? "bg-primary/5 border-l-2 border-l-primary"
                            : "hover:bg-slate-100/50"
                            }`}
                    >
                        <div className="flex items-center gap-2.5">
                            <div className="relative shrink-0">
                                <div className={`w-9 h-9 rounded-full flex items-center justify-center text-white text-xs font-bold ${conv.unreadCount > 0 ? "bg-primary" : "bg-slate-400"
                                    }`}>
                                    {conv.otherUser.avatar_url ? (
                                        <img src={conv.otherUser.avatar_url} alt="" className="w-full h-full object-cover rounded-full" />
                                    ) : (
                                        (conv.otherUser.name || "?")[0]?.toUpperCase()
                                    )}
                                </div>
                                <span className={`absolute -bottom-0.5 -right-0.5 w-2.5 h-2.5 rounded-full border-2 border-slate-50 ${onlineUsers.has(conv.otherUser.id) ? "bg-emerald-400" : "bg-slate-300"}`} />
                            </div>
                            <div className="flex-1 min-w-0">
                                <div className="flex justify-between items-center">
                                    <span className={`text-xs truncate ${conv.unreadCount > 0 ? "font-bold text-slate-900" : "font-medium text-slate-700"
                                        }`}>
                                        {conv.otherUser.name}
                                    </span>
                                    {conv.unreadCount > 0 && (
                                        <span className="flex items-center justify-center w-4 h-4 rounded-full bg-primary text-[10px] text-white font-bold shrink-0">
                                            {conv.unreadCount}
                                        </span>
                                    )}
                                </div>
                                <p className="text-[10px] text-slate-400 truncate mt-0.5">{conv.jobTitle}</p>
                                {conv.lastMessage ? (
                                    <div className="flex justify-between items-end mt-1 gap-2">
                                        <p className={`text-[11px] truncate ${conv.unreadCount > 0 ? "text-slate-700 font-medium" : "text-slate-500"}`}>
                                            {conv.lastMessage.content}
                                        </p>
                                        <span className="text-[9px] text-slate-400 shrink-0 mb-0.5">
                                            {new Date(conv.lastMessage.createdAt).toLocaleDateString([], { month: 'short', day: 'numeric' })}
                                        </span>
                                    </div>
                                ) : (
                                    <p className="text-[11px] text-slate-300 italic mt-1">No messages yet</p>
                                )}
                            </div>
                        </div>
                    </Link>
                ))}
            </div>
        </div>
    );
}
