import Link from "next/link";
import { Mail, Phone, MapPin, Briefcase, ExternalLink, Calendar, Clock, Video, ChevronRight, FileText } from "lucide-react";

export interface ThreadData {
    applicationId: string;
    applicationStatus: string;
    jobTitle: string;
    jobId: string;
    currentUserId: string;
    otherUser: {
        id: string;
        name: string;
        role: string;
        avatar_url: string | null;
        email?: string;
        headline?: string;
        company?: string;
        location?: string;
        phone?: string;
    };
    upcomingInterview: any | null;
}

const statusColors: Record<string, string> = {
    pending: "bg-amber-100 text-amber-700",
    reviewing: "bg-blue-100 text-blue-700",
    interview: "bg-purple-100 text-purple-700",
    offered: "bg-emerald-100 text-emerald-700",
    hired: "bg-emerald-200 text-emerald-800",
    rejected: "bg-red-100 text-red-700",
};

export function ConversationSidebar({ thread }: { thread: ThreadData }) {
    return (
        <div className="w-[280px] shrink-0 border-l border-slate-100 overflow-y-auto hidden xl:block bg-white">
            <div className="p-5 space-y-5">
                {/* Person Card */}
                <div className="text-center">
                    {thread.otherUser.avatar_url ? (
                        <img src={thread.otherUser.avatar_url} alt="" className="w-16 h-16 rounded-full mx-auto object-cover border-2 border-slate-100" />
                    ) : (
                        <div className="w-16 h-16 rounded-full bg-primary mx-auto flex items-center justify-center text-white font-bold text-xl">
                            {(thread.otherUser.name || "?")[0]?.toUpperCase()}
                        </div>
                    )}
                    <h3 className="font-bold text-slate-900 mt-3">{thread.otherUser.name}</h3>
                    {thread.otherUser.headline && (
                        <p className="text-xs text-slate-500 mt-0.5">{thread.otherUser.headline}</p>
                    )}
                </div>

                {/* Contact Info */}
                <div className="space-y-2">
                    {thread.otherUser.email && (
                        <div className="flex items-center gap-2 text-xs text-slate-600">
                            <Mail className="w-3.5 h-3.5 text-slate-400" />
                            <span className="truncate">{thread.otherUser.email}</span>
                        </div>
                    )}
                    {thread.otherUser.phone && (
                        <div className="flex items-center gap-2 text-xs text-slate-600">
                            <Phone className="w-3.5 h-3.5 text-slate-400" />
                            {thread.otherUser.phone}
                        </div>
                    )}
                    {thread.otherUser.location && (
                        <div className="flex items-center gap-2 text-xs text-slate-600">
                            <MapPin className="w-3.5 h-3.5 text-slate-400" />
                            {thread.otherUser.location}
                        </div>
                    )}
                </div>

                <hr className="border-slate-100" />

                {/* Application Status */}
                <div className="bg-slate-50 rounded-xl p-3.5 space-y-2">
                    <h4 className="text-[11px] font-bold text-slate-400 uppercase tracking-wider">Application</h4>
                    <p className="text-sm font-semibold text-slate-800">{thread.jobTitle}</p>
                    <span className={`inline-block text-[11px] font-semibold px-2 py-0.5 rounded-full ${statusColors[thread.applicationStatus] || "bg-slate-100 text-slate-600"}`}>
                        {thread.applicationStatus.replace('_', ' ')}
                    </span>
                    <Link
                        href={`/dashboard/applications/${thread.applicationId}`}
                        className="flex items-center gap-1 text-xs text-primary font-semibold hover:underline mt-2"
                    >
                        View Full Application
                        <ExternalLink className="w-3 h-3" />
                    </Link>
                </div>

                {/* Upcoming Interview */}
                {thread.upcomingInterview && (
                    <div className="bg-purple-50 rounded-xl p-3.5 space-y-2">
                        <h4 className="text-[11px] font-bold text-purple-400 uppercase tracking-wider">Upcoming Interview</h4>
                        <div className="flex items-center gap-1.5 text-xs text-purple-700">
                            <Calendar className="w-3.5 h-3.5" />
                            {new Date(thread.upcomingInterview.scheduled_at).toLocaleDateString("en-US", {
                                month: "short", day: "numeric", year: "numeric"
                            })}
                        </div>
                        <div className="flex items-center gap-1.5 text-xs text-purple-700">
                            <Clock className="w-3.5 h-3.5" />
                            {new Date(thread.upcomingInterview.scheduled_at).toLocaleTimeString("en-US", {
                                hour: "numeric", minute: "2-digit"
                            })}
                            {" · "}{thread.upcomingInterview.duration_minutes} min
                        </div>
                        {thread.upcomingInterview.meeting_link && (
                            <div className="flex items-center gap-1.5 text-xs text-purple-700">
                                <Video className="w-3.5 h-3.5" />
                                <a href={thread.upcomingInterview.meeting_link} target="_blank" rel="noopener" className="underline">
                                    Join Meeting
                                </a>
                            </div>
                        )}
                    </div>
                )}

                {/* Quick Actions */}
                <div className="space-y-1.5">
                    <h4 className="text-[11px] font-bold text-slate-400 uppercase tracking-wider mb-2">Quick Actions</h4>
                    <Link
                        href={`/dashboard/applications/${thread.applicationId}`}
                        className="flex items-center gap-2 px-3 py-2 text-xs font-semibold text-slate-600 hover:bg-slate-50 rounded-lg transition-colors"
                    >
                        <FileText className="w-4 h-4 text-slate-400" />
                        View Application
                        <ChevronRight className="w-3 h-3 ml-auto text-slate-300" />
                    </Link>
                </div>
            </div>
        </div>
    );
}
