import { atom } from "jotai";
import { atomWithStorage } from "jotai/utils";

/**
 * Persisted floating AI chat window geometry (position + size). Held in
 * localStorage so a drag/resize survives a full page reload. `null` means
 * "never placed yet" — the window then computes an initial top-right placement.
 * On restore the value is clamped to the current viewport (see AiChatWindow).
 */
export type AiChatWindowGeom = {
  left: number;
  top: number;
  width: number;
  height: number;
};
export const aiChatWindowGeomAtom = atomWithStorage<AiChatWindowGeom | null>(
  "ai-chat-window-geom",
  null,
);

/**
 * Whether the AI chat window is docked into the sidebar (page-tree navbar).
 * Persisted to localStorage so the docked/floating mode survives a full page
 * reload and close/reopen. `false` = the default floating window. When docked,
 * the SAME window instance pins itself to the live bounding rect of the app
 * navbar (see AiChatWindow), overlaying the page tree.
 */
export const aiChatWindowDockedAtom = atomWithStorage<boolean>(
  "ai-chat-window-docked",
  false,
);

/**
 * The currently selected chat id. `null` means a fresh (not-yet-created) chat:
 * the server creates the chat row on the first streamed message and echoes its
 * id, which the panel then adopts.
 */
// Note: declare via a cast default rather than `atom<string | null>(null)`,
// which mis-resolves the jotai useAtom overload to the read-only signature
// under this TS/jotai version (the setter would type as `never`).
export const activeAiChatIdAtom = atom(null as string | null);

// Whether the floating AI chat window is open. Non-persistent (resets per session).
export const aiChatWindowOpenAtom = atom<boolean>(false);

/**
 * The agent role selected for the NEXT new chat. `null` = "Universal assistant"
 * (no role). Consulted ONLY when creating a chat (its first message): the server
 * persists it to ai_chats.role_id and the role is immutable afterwards. Reset to
 * null when starting a new chat. It does NOT affect already-created chats.
 */
// Cast default for the same jotai overload reason as activeAiChatIdAtom above.
export const selectedAiRoleIdAtom = atom(null as string | null);

// The AI chat composer draft (text typed but not yet sent). Held here — OUTSIDE
// ChatThread — so it survives the thread remount that happens when a brand-new
// chat adopts its freshly created id after the first turn finishes. If it lived
// in ChatInput's local state, that remount would wipe text the user typed while
// the agent was still streaming. Reset on deliberate chat switches.
export const aiChatDraftAtom = atom<string>("");
