import { getCurrentUser } from "@/lib/auth";
import { redirect, notFound } from "next/navigation";
import { getConversationThread, getConversations } from "@/app/actions/conversationActions";
import { MessageThreadView } from "@/components/messages/MessageThreadView";

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

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

    let thread;
    try {
        thread = await getConversationThread(params.applicationId);
    } catch (error) {
        notFound();
    }

    // Get conversations for sidebar list
    const conversations = await getConversations();

    return (
        <MessageThreadView
            thread={thread}
            conversations={conversations}
            currentUserId={currentUser.id}
            currentUserRole={currentUser.role}
        />
    );
}
