import { useState } from "react";
import { useTranslations } from "next-intl";
import { toast } from "sonner";
import type { Conversation } from "./conversation-menu";

/** Shared fetch/rename/delete logic for the general assistant's conversation
 *  list — used by both the dropdown (ConversationMenu, narrow/mobile) and the
 *  persistent sidebar (desktop fullscreen) skins, so the two never diverge. */
export function useConversationList(onDeletedActive: () => void, activeId: string | null) {
  const t = useTranslations("conversations");
  const [conversations, setConversations] = useState<Conversation[]>([]);
  const [loading, setLoading] = useState(false);
  const [loaded, setLoaded] = useState(false);
  const [renamingId, setRenamingId] = useState<string | null>(null);
  const [renameValue, setRenameValue] = useState("");
  const [deleteTargetId, setDeleteTargetId] = useState<string | null>(null);

  async function load() {
    if (loaded) return;
    setLoaded(true);
    setLoading(true);
    try {
      const res = await fetch("/api/v1/ai/conversations");
      if (!res.ok) return;
      const json = await res.json() as { data: Conversation[] };
      setConversations(json.data);
    } finally {
      setLoading(false);
    }
  }

  async function create(): Promise<Conversation | null> {
    const res = await fetch("/api/v1/ai/conversations", { method: "POST" });
    if (!res.ok) {
      toast.error(t("createFailed"));
      return null;
    }
    const created = await res.json() as Conversation;
    setConversations((prev) => [created, ...prev]);
    return created;
  }

  function startRename(conversation: Conversation) {
    setRenamingId(conversation.id);
    setRenameValue(conversation.title ?? "");
  }

  async function submitRename() {
    if (!renamingId) return;
    const id = renamingId;
    const title = renameValue.trim() || null;
    setRenamingId(null);
    const res = await fetch(`/api/v1/ai/conversations/${id}`, {
      method: "PATCH",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ title }),
    });
    if (!res.ok) {
      toast.error(t("renameFailed"));
      return;
    }
    setConversations((prev) => prev.map((c) => (c.id === id ? { ...c, title } : c)));
  }

  async function confirmDelete() {
    const id = deleteTargetId;
    setDeleteTargetId(null);
    if (!id) return;
    const res = await fetch(`/api/v1/ai/conversations/${id}`, { method: "DELETE" });
    if (!res.ok) {
      toast.error(t("deleteFailed"));
      return;
    }
    setConversations((prev) => prev.filter((c) => c.id !== id));
    if (id === activeId) onDeletedActive();
  }

  return {
    t,
    conversations,
    loading,
    load,
    create,
    renamingId,
    renameValue,
    setRenameValue,
    startRename,
    cancelRename: () => setRenamingId(null),
    submitRename,
    deleteTargetId,
    setDeleteTargetId,
    confirmDelete,
  };
}
