import { getCurrentUser } from "@/lib/auth";
import { redirect, notFound } from "next/navigation";
import { MessageSquare, Send, ArrowLeft, User } from "lucide-react";
import Link from "next/link";

export default async function MessageDetailPage(props: { params: Promise<{ id: string }> }) {
    const params = await props.params;
    const currentUser = await getCurrentUser();

    if (!currentUser) {
        redirect("/login");
    }

    // Mock conversation data - will be replaced with real database queries
    const conversations: any = {
        "conv1": {
            id: "conv1",
            otherUser: {
                name: "Sarah Miller",
                role: "employer",
                company: "Acme Corp",
                avatar: "S"
            },
            messages: [
                {
                    id: "msg1",
                    senderId: params.id,
                    content: "Thank you for your application. We'd like to schedule an interview.",
                    timestamp: "2026-02-10T10:00:00Z"
                },
                {
                    id: "msg2",
                    senderId: currentUser.id,
                    content: "Thank you for considering my application! I'd be happy to interview. What times work best for you?",
                    timestamp: "2026-02-10T10:15:00Z"
                },
                {
                    id: "msg3",
                    senderId: params.id,
                    content: "How about next Tuesday at 2:00 PM?",
                    timestamp: "2026-02-10T10:30:00Z"
                }
            ]
        },
        "conv2": {
            id: "conv2",
            otherUser: {
                name: "Alex Riviera",
                role: "candidate",
                title: "Senior Frontend Developer",
                avatar: "A"
            },
            messages: [
                {
                    id: "msg1",
                    senderId: params.id,
                    content: "I'm very interested in this position and would love to discuss further.",
                    timestamp: "2026-02-09T14:00:00Z"
                }
            ]
        }
    };

    const conversation = conversations[params.id];

    if (!conversation) {
        notFound();
    }

    return (
        <div className="h-[calc(100vh-4rem)] flex flex-col">
            {/* Header */}
            <div className="border-b border-slate-200 bg-white p-4">
                <div className="flex items-center gap-4">
                    <Link
                        href="/dashboard/messages"
                        className="p-2 hover:bg-slate-100 rounded-lg transition-colors lg:hidden"
                    >
                        <ArrowLeft className="h-5 w-5 text-slate-600" />
                    </Link>

                    <div className="h-10 w-10 rounded-full bg-primary text-white font-bold flex items-center justify-center">
                        {conversation.otherUser.avatar}
                    </div>

                    <div className="flex-1">
                        <h2 className="font-bold text-slate-900">{conversation.otherUser.name}</h2>
                        <p className="text-sm text-slate-500">
                            {conversation.otherUser.role === "employer"
                                ? conversation.otherUser.company
                                : conversation.otherUser.title
                            }
                        </p>
                    </div>

                    <button className="p-2 hover:bg-slate-100 rounded-lg transition-colors">
                        <User className="h-5 w-5 text-slate-600" />
                    </button>
                </div>
            </div>

            {/* Messages */}
            <div className="flex-1 overflow-y-auto bg-slate-50/50 p-6">
                <div className="max-w-4xl mx-auto space-y-4">
                    {conversation.messages.map((message: any) => {
                        const isOwnMessage = message.senderId === currentUser.id;
                        return (
                            <div
                                key={message.id}
                                className={`flex ${isOwnMessage ? 'justify-end' : 'justify-start'}`}
                            >
                                <div className={`max-w-md ${isOwnMessage ? 'order-2' : 'order-1'}`}>
                                    <div
                                        className={`rounded-2xl px-4 py-3 ${isOwnMessage
                                                ? 'bg-primary text-white'
                                                : 'bg-white border border-slate-100 text-slate-900'
                                            }`}
                                    >
                                        <p className="text-sm leading-relaxed">{message.content}</p>
                                    </div>
                                    <p className="text-xs text-slate-400 mt-1 px-1">
                                        {new Date(message.timestamp).toLocaleTimeString([], {
                                            hour: '2-digit',
                                            minute: '2-digit'
                                        })}
                                    </p>
                                </div>
                            </div>
                        );
                    })}
                </div>
            </div>

            {/* Input */}
            <div className="border-t border-slate-200 bg-white p-4">
                <div className="max-w-4xl mx-auto">
                    <form className="flex items-center gap-3">
                        <input
                            type="text"
                            placeholder="Type your message..."
                            className="flex-1 rounded-full border border-slate-200 px-4 py-3 outline-none focus:border-primary focus:ring-1 focus:ring-primary/20 transition-all"
                        />
                        <button
                            type="submit"
                            className="h-12 w-12 rounded-full bg-primary text-white flex items-center justify-center hover:bg-blue-700 transition-colors shrink-0"
                        >
                            <Send className="h-5 w-5" />
                        </button>
                    </form>
                </div>
            </div>

            {/* Feature Notice */}
            <div className="border-t border-amber-200 bg-amber-50 p-3">
                <div className="max-w-4xl mx-auto">
                    <div className="flex items-center gap-2 text-sm">
                        <MessageSquare className="h-4 w-4 text-amber-600" />
                        <p className="text-amber-700">
                            <span className="font-semibold">Development Mode:</span> Full messaging functionality coming soon. Messages are currently for demonstration only.
                        </p>
                    </div>
                </div>
            </div>
        </div>
    );
}
