"use client";

import type { CloudThread } from "@assistant-ui/cloud-ai-sdk";
import { cn } from "@/lib/utils";
import { Plus, Trash2, MessageSquare } from "lucide-react";

type ThreadListProps = {
  threads: CloudThread[];
  selectedId: string | null;
  onSelect: (id: string | null) => void;
  onDelete: (id: string) => void;
  isLoading: boolean;
};

export function ThreadList({
  threads,
  selectedId,
  onSelect,
  onDelete,
  isLoading,
}: ThreadListProps) {
  return (
    <div className="bg-sidebar flex h-full w-64 shrink-0 flex-col border-r">
      <div className="p-3">
        <button
          type="button"
          onClick={() => onSelect(null)}
          className="border-sidebar-border bg-sidebar text-sidebar-foreground hover:bg-sidebar-accent flex w-full items-center justify-center gap-2 rounded-lg border px-4 py-2 text-sm font-medium transition-colors"
        >
          <Plus className="size-4" />
          New Chat
        </button>
      </div>

      <div className="flex-1 overflow-y-auto px-2 pb-2">
        {isLoading && threads.length === 0 ? (
          <div className="text-muted-foreground flex items-center justify-center py-8 text-sm">
            Loading...
          </div>
        ) : threads.length === 0 ? (
          <div className="text-muted-foreground flex flex-col items-center justify-center gap-2 py-8 text-center text-sm">
            <MessageSquare className="size-6 opacity-40" />
            <p>No conversations yet</p>
          </div>
        ) : (
          <div className="space-y-0.5">
            {threads.map((thread) => (
              <button
                type="button"
                key={thread.id}
                onClick={() => onSelect(thread.id)}
                className={cn(
                  "group flex w-full cursor-pointer items-center gap-2 rounded-lg px-3 py-2 text-left text-sm transition-colors",
                  selectedId === thread.id
                    ? "bg-sidebar-accent text-sidebar-accent-foreground"
                    : "text-sidebar-foreground hover:bg-sidebar-accent/50",
                )}
              >
                <span className="flex-1 truncate">
                  {thread.title || "New conversation"}
                </span>
                <button
                  type="button"
                  onClick={(e) => {
                    e.stopPropagation();
                    onDelete(thread.id);
                  }}
                  className="shrink-0 opacity-0 transition-opacity group-hover:opacity-100"
                  aria-label="Delete thread"
                >
                  <Trash2 className="text-muted-foreground hover:text-destructive size-3.5" />
                </button>
              </button>
            ))}
          </div>
        )}
      </div>
    </div>
  );
}
