/**
 * Session store actions for managing terminal tabs (add, remove, rename, reorder, activate).
 */
import type { Session, TerminalTab } from './sessions'
import { debouncedSave } from './sessionPersistence'

type StoreGet = () => {
  sessions: Session[]
}
type StoreSet = (partial: { sessions: Session[] }) => void

export function createTerminalTabActions(get: StoreGet, set: StoreSet) {
  return {
    addTerminalTab: (sessionId: string, name?: string, isolated?: boolean): string => {
      const { sessions } = get()
      const tabId = `tab-${Date.now()}-${Math.random().toString(36).substring(2, 11)}`
      const session = sessions.find((s) => s.id === sessionId)
      const tabNumber = session ? session.terminalTabs.tabs.length + 1 : 1
      const defaultName = isolated ? `Container ${tabNumber}` : `Terminal ${tabNumber}`
      const tabName = name || defaultName
      const newTab: { id: string; name: string; isolated?: boolean } = { id: tabId, name: tabName }
      if (isolated) newTab.isolated = true

      const updatedSessions = sessions.map((s) => {
        if (s.id !== sessionId) return s
        return {
          ...s,
          terminalTabs: {
            tabs: [...s.terminalTabs.tabs, newTab],
            activeTabId: tabId,
          },
        }
      })
      set({ sessions: updatedSessions })
      debouncedSave()
      return tabId
    },

    removeTerminalTab: (sessionId: string, tabId: string) => {
      const { sessions } = get()
      const updatedSessions = sessions.map((s) => {
        if (s.id !== sessionId) return s
        const tabIndex = s.terminalTabs.tabs.findIndex((t) => t.id === tabId)
        const newTabs = s.terminalTabs.tabs.filter((t) => t.id !== tabId)

        // Don't allow closing the last tab
        if (newTabs.length === 0) return s

        // If closing the active tab, select an adjacent one
        let newActiveId = s.terminalTabs.activeTabId
        if (s.terminalTabs.activeTabId === tabId) {
          // Prefer the tab to the right, or the one to the left if closing the rightmost
          const newIndex = Math.min(tabIndex, newTabs.length - 1)
          newActiveId = newTabs[newIndex].id
        }

        return {
          ...s,
          terminalTabs: {
            tabs: newTabs,
            activeTabId: newActiveId,
          },
        }
      })
      set({ sessions: updatedSessions })
      debouncedSave()
    },

    renameTerminalTab: (sessionId: string, tabId: string, name: string) => {
      const { sessions } = get()
      const updatedSessions = sessions.map((s) => {
        if (s.id !== sessionId) return s
        return {
          ...s,
          terminalTabs: {
            ...s.terminalTabs,
            tabs: s.terminalTabs.tabs.map((t) =>
              t.id === tabId ? { ...t, name } : t
            ),
          },
        }
      })
      set({ sessions: updatedSessions })
      debouncedSave()
    },

    reorderTerminalTabs: (sessionId: string, tabs: TerminalTab[]) => {
      const { sessions } = get()
      const updatedSessions = sessions.map((s) => {
        if (s.id !== sessionId) return s
        return {
          ...s,
          terminalTabs: {
            ...s.terminalTabs,
            tabs,
          },
        }
      })
      set({ sessions: updatedSessions })
      debouncedSave()
    },

    setActiveTerminalTab: (sessionId: string, tabId: string) => {
      const { sessions } = get()
      const updatedSessions = sessions.map((s) => {
        if (s.id !== sessionId) return s
        return {
          ...s,
          terminalTabs: {
            ...s.terminalTabs,
            activeTabId: tabId,
          },
        }
      })
      set({ sessions: updatedSessions })
      // Don't persist active tab - it's runtime state
    },

    closeOtherTerminalTabs: (sessionId: string, tabId: string) => {
      const { sessions } = get()
      const updatedSessions = sessions.map((s) => {
        if (s.id !== sessionId) return s
        const tab = s.terminalTabs.tabs.find((t) => t.id === tabId)
        if (!tab) return s
        return {
          ...s,
          terminalTabs: {
            tabs: [tab],
            activeTabId: tabId,
          },
        }
      })
      set({ sessions: updatedSessions })
      debouncedSave()
    },

    closeTerminalTabsToRight: (sessionId: string, tabId: string) => {
      const { sessions } = get()
      const updatedSessions = sessions.map((s) => {
        if (s.id !== sessionId) return s
        const tabIndex = s.terminalTabs.tabs.findIndex((t) => t.id === tabId)
        if (tabIndex === -1) return s
        const newTabs = s.terminalTabs.tabs.slice(0, tabIndex + 1)
        // If active tab was to the right, select the clicked tab
        const activeIndex = s.terminalTabs.tabs.findIndex((t) => t.id === s.terminalTabs.activeTabId)
        const newActiveId = activeIndex > tabIndex ? tabId : s.terminalTabs.activeTabId
        return {
          ...s,
          terminalTabs: {
            tabs: newTabs,
            activeTabId: newActiveId,
          },
        }
      })
      set({ sessions: updatedSessions })
      debouncedSave()
    },
  }
}
