import { derived } from "svelte/store";
import { conversationsStore } from "./conversations";
import type { TerminalLine } from "$lib/types/messages";
import { characterState } from "$lib/stores/character";
import {
  setShouldRestoreHistory,
  setSavedHistory,
  getShouldRestoreHistory,
  getSavedHistory,
  clearHistoryRestore,
} from "./historyRestore";

// Re-export TerminalLine type for backwards compatibility
export type { TerminalLine };

// Re-export from conversations store for backwards compatibility
export const claudeStore = {
  // Existing subscriptions
  connectionStatus: conversationsStore.connectionStatus,
  sessionId: conversationsStore.sessionId,
  currentWorkingDirectory: conversationsStore.currentWorkingDirectory,
  terminalLines: conversationsStore.terminalLines,
  pendingPermission: conversationsStore.pendingPermission,
  pendingQuestion: conversationsStore.pendingQuestion,
  isProcessing: conversationsStore.isProcessing,
  grantedTools: conversationsStore.grantedTools,
  pendingRetryMessage: conversationsStore.pendingRetryMessage,
  attachments: conversationsStore.attachments,

  // New conversation-aware subscriptions
  conversations: conversationsStore.conversations,
  activeConversationId: conversationsStore.activeConversationId,
  activeConversation: conversationsStore.activeConversation,

  // Methods
  setConnectionStatus: conversationsStore.setConnectionStatus,
  setConnectionStatusForConversation: conversationsStore.setConnectionStatusForConversation,
  setCharacterStateForConversation: conversationsStore.setCharacterStateForConversation,
  setSessionId: conversationsStore.setSessionId,
  setSessionIdForConversation: conversationsStore.setSessionIdForConversation,
  setWorkingDirectory: conversationsStore.setWorkingDirectory,
  setWorkingDirectoryForConversation: conversationsStore.setWorkingDirectoryForConversation,
  setProcessing: conversationsStore.setProcessing,
  addLine: conversationsStore.addLine,
  addLineToConversation: conversationsStore.addLineToConversation,
  updateLine: conversationsStore.updateLine,
  appendToLine: conversationsStore.appendToLine,
  clearTerminal: conversationsStore.clearTerminal,
  getConversationHistory: conversationsStore.getConversationHistory,
  requestPermission: conversationsStore.requestPermission,
  clearPermission: conversationsStore.clearPermission,
  requestPermissionForConversation: conversationsStore.requestPermissionForConversation,
  clearPermissionForConversation: conversationsStore.clearPermissionForConversation,
  requestQuestion: conversationsStore.requestQuestion,
  clearQuestion: conversationsStore.clearQuestion,
  requestQuestionForConversation: conversationsStore.requestQuestionForConversation,
  clearQuestionForConversation: conversationsStore.clearQuestionForConversation,
  grantTool: conversationsStore.grantTool,
  revokeAllTools: conversationsStore.revokeAllTools,
  isToolGranted: conversationsStore.isToolGranted,
  setPendingRetryMessage: conversationsStore.setPendingRetryMessage,

  // Conversation management
  createConversation: conversationsStore.createConversation,
  deleteConversation: conversationsStore.deleteConversation,
  switchConversation: conversationsStore.switchConversation,
  renameConversation: conversationsStore.renameConversation,
  saveScrollPosition: conversationsStore.saveScrollPosition,
  getScrollPosition: conversationsStore.getScrollPosition,

  // Attachment management
  addAttachment: conversationsStore.addAttachment,
  removeAttachment: conversationsStore.removeAttachment,
  clearAttachments: conversationsStore.clearAttachments,
  getAttachments: conversationsStore.getAttachments,

  getGrantedTools: (): string[] => {
    let tools: string[] = [];
    conversationsStore.grantedTools.subscribe((t) => (tools = Array.from(t)))();
    return tools;
  },

  // History restoration methods from main branch
  setShouldRestoreHistory: setShouldRestoreHistory,
  setSavedConversationHistory: setSavedHistory,
  getShouldRestoreHistory: getShouldRestoreHistory,
  getSavedConversationHistory: getSavedHistory,

  reset: () => {
    // Reset only the active conversation
    conversationsStore.clearTerminal();
    conversationsStore.setSessionId(null);
    conversationsStore.setWorkingDirectory("");
    conversationsStore.setProcessing(false);
    conversationsStore.revokeAllTools();
    conversationsStore.clearAttachments();
    // Also clear history restoration
    clearHistoryRestore();
  },
};

export const hasPermissionPending = derived(
  claudeStore.activeConversation,
  ($conversation) => $conversation?.pendingPermission !== null
);

export const hasQuestionPending = derived(
  claudeStore.activeConversation,
  ($conversation) => $conversation?.pendingQuestion !== null
);

// Derived store to check if Claude is currently processing (can be interrupted)
export const isClaudeProcessing = derived(
  [claudeStore.connectionStatus, characterState],
  ([$connectionStatus, $characterState]) => {
    // Must be connected and in one of the processing states
    return (
      $connectionStatus === "connected" &&
      ["thinking", "typing", "searching", "coding", "mcp"].includes($characterState)
    );
  }
);
