"use client";

import { useRouter } from "next/navigation";
import { useThreadList, useSdk } from "@lynkd/messaging-inbox-sdk";
import { useUi } from "@/lib/ui-state";
import { Header } from "@/components/nav/Header";
import { Avatar } from "@/components/ui/primitives";
import { RichText } from "@/lib/rich-text";
import { relativeTime } from "@/lib/format";
import { MessagesSquare, Hash } from "lucide-react";

export function ThreadsView() {
  const { messages, loading } = useThreadList();
  const { userById, channelById } = useSdk();
  const { openThread } = useUi();
  const router = useRouter();

  const open = (channelId: string, parentId: string) => {
    router.push(`/c/${channelId}`);
    openThread(channelId, parentId);
  };

  return (
    <div className="flex min-w-0 flex-1 flex-col">
      <Header eyebrow="Messaging" title="Threads" subtitle="Conversations you're following" />
      <div className="min-h-0 flex-1 overflow-y-auto p-4">
        {loading && <div className="py-10 text-center text-muted">Loading…</div>}
        {!loading && messages.length === 0 && (
          <div className="flex flex-col items-center gap-2 py-20 text-muted">
            <MessagesSquare size={34} className="text-dim" />
            <div className="text-[15px] font-semibold text-ink">No threads yet</div>
            <div className="text-[13px]">Reply in a thread on any message and it shows up here.</div>
          </div>
        )}
        <div className="mx-auto max-w-3xl space-y-2">
          {messages.map((m) => {
            const author = userById(m.authorId);
            const ch = m.channelId ? channelById(m.channelId) : undefined;
            return (
              <button
                key={m.id}
                onClick={() => ch && open(ch.id, m.id)}
                className="flex w-full items-start gap-3 rounded-card border border-border bg-panel p-3.5 text-left transition-colors hover:border-border-strong"
              >
                {author && <Avatar user={author} size={34} showPresence={false} />}
                <div className="min-w-0 flex-1">
                  <div className="flex items-center gap-2 text-[12.5px]">
                    <b>{author?.name}</b>
                    {ch && <span className="flex items-center gap-0.5 text-muted"><Hash size={11} /> {ch.name}</span>}
                    <span className="ml-auto text-[11px] text-dim">{relativeTime(m.ts)}</span>
                  </div>
                  <div className="mt-0.5 line-clamp-2 text-[13.5px] text-ink/85"><RichText text={m.body} /></div>
                  <div className="mt-1.5 flex items-center gap-2 text-[12px] font-semibold text-info">
                    <span className="flex -space-x-1.5">
                      {(m.replyUserIds ?? []).slice(0, 3).map((id) => {
                        const u = userById(id);
                        return u ? <Avatar key={id} user={u} size={16} showPresence={false} rounded="5px" /> : null;
                      })}
                    </span>
                    {m.replyCount} {m.replyCount === 1 ? "reply" : "replies"}
                    {m.lastReplyTs && <span className="font-normal text-dim">· last {relativeTime(m.lastReplyTs)}</span>}
                  </div>
                </div>
              </button>
            );
          })}
        </div>
      </div>
    </div>
  );
}
