import { create } from "zustand"
import { persist } from "zustand/middleware"

export type PaneType =
  | "chat"
  | "code"
  | "diff"
  | "design"
  | "explorer"
  | "tasks"

export interface PaneInstance {
  id: string
  type: PaneType
  visible: boolean
  order: number
  size: number
  minSize: number
  maxSize: number
}

export interface PaneState {
  panes: PaneInstance[]
  focusedPaneId: string | null
  sideChatOpen: boolean
  sessionSidebarOpen: boolean

  registerPane: (pane: PaneInstance) => void
  unregisterPane: (id: string) => void
  ensurePane: (type: PaneType, defaults?: Partial<PaneInstance>) => void
  togglePane: (id: string) => void
  setPaneVisibility: (id: string, visible: boolean) => void
  setPaneSize: (id: string, size: number) => void
  reorderPanes: (ids: string[]) => void
  setPaneOrder: (id: string, order: number) => void
  focusPane: (id: string) => void
  toggleSideChat: () => void
  setSideChatOpen: (open: boolean) => void
  toggleSessionSidebar: () => void
  setSessionSidebarOpen: (open: boolean) => void
  getPane: (type: PaneType) => PaneInstance | undefined
  getVisiblePanes: () => PaneInstance[]
}

const DEFAULT_PANES: PaneInstance[] = [
  { id: "explorer", type: "explorer", visible: true, order: 0, size: 240, minSize: 180, maxSize: 400 },
  { id: "chat", type: "chat", visible: true, order: 1, size: 1, minSize: 300, maxSize: Infinity },
  { id: "code", type: "code", visible: true, order: 2, size: 480, minSize: 300, maxSize: Infinity },
  { id: "diff", type: "diff", visible: false, order: 3, size: 480, minSize: 300, maxSize: Infinity },
  { id: "design", type: "design", visible: false, order: 4, size: 480, minSize: 300, maxSize: Infinity },
]

export const usePaneStore = create<PaneState>()(
  persist(
    (set, get) => ({
      panes: DEFAULT_PANES,
      focusedPaneId: null,
      sideChatOpen: false,
      sessionSidebarOpen: false,

      registerPane: (pane) =>
        set((s) => {
          const exists = s.panes.find((p) => p.id === pane.id)
          if (exists) return s
          return { panes: [...s.panes, pane] }
        }),

      ensurePane: (type, defaults) =>
        set((s) => {
          const existing = s.panes.find((p) => p.type === type)
          if (existing) {
            if (!existing.visible) {
              return { panes: s.panes.map((p) => p.id === existing.id ? { ...p, visible: true } : p) }
            }
            return s
          }
          const id = type
          const maxOrder = Math.max(...s.panes.map((p) => p.order), 0)
          const pane: PaneInstance = {
            id,
            type,
            visible: true,
            order: maxOrder + 1,
            size: 480,
            minSize: 200,
            maxSize: Infinity,
            ...defaults,
          }
          return { panes: [...s.panes, pane] }
        }),

      unregisterPane: (id) =>
        set((s) => ({ panes: s.panes.filter((p) => p.id !== id) })),

      togglePane: (id) =>
        set((s) => ({
          panes: s.panes.map((p) =>
            p.id === id ? { ...p, visible: !p.visible } : p
          ),
        })),

      setPaneVisibility: (id, visible) =>
        set((s) => ({
          panes: s.panes.map((p) =>
            p.id === id ? { ...p, visible } : p
          ),
        })),

      setPaneSize: (id, size) =>
        set((s) => ({
          panes: s.panes.map((p) =>
            p.id === id
              ? { ...p, size: Math.max(p.minSize, Math.min(p.maxSize, size)) }
              : p
          ),
        })),

      reorderPanes: (ids) =>
        set((s) => {
          const paneMap = new Map(s.panes.map((p) => [p.id, p]))
          return {
            panes: ids.map((id, i) => {
              const p = paneMap.get(id)
              return p ? { ...p, order: i } : ({ id, type: "chat" as PaneType, visible: true, order: i, size: 1, minSize: 100, maxSize: Infinity } as any)
            }).filter(Boolean),
          }
        }),

      setPaneOrder: (id, order) =>
        set((s) => ({
          panes: s.panes.map((p) =>
            p.id === id ? { ...p, order } : p
          ),
        })),

      focusPane: (id) => set({ focusedPaneId: id }),

      toggleSideChat: () =>
        set((s) => ({ sideChatOpen: !s.sideChatOpen })),

      setSideChatOpen: (open) => set({ sideChatOpen: open }),

      toggleSessionSidebar: () =>
        set((s) => ({ sessionSidebarOpen: !s.sessionSidebarOpen })),

      setSessionSidebarOpen: (open) => set({ sessionSidebarOpen: open }),

      getPane: (type) => get().panes.find((p) => p.type === type),

      getVisiblePanes: () =>
        get()
          .panes.filter((p) => p.visible)
          .sort((a, b) => a.order - b.order),
    }),
    {
      name: "aos-pane-store",
      partialize: (state) => ({
        panes: state.panes,
      }),
    }
  )
)
