import { create } from 'zustand'
import { WorkspaceType } from '../domain/enums'

export const RightPanelContentType = {
  AI_CONTEXT:      'ai_context',
  DETAIL_PREVIEW:  'detail_preview',
  COMPARE_PREVIEW: 'compare_preview',
  ACTIVITY_FEED:   'activity_feed',
} as const
export type RightPanelContentType = typeof RightPanelContentType[keyof typeof RightPanelContentType]

interface LayoutState {
  activeWorkspace: WorkspaceType
  sidebarCollapsed: boolean
  isRightPanelOpen: boolean
  rightPanelContentType: RightPanelContentType | null
  // Actions
  setActiveWorkspace: (workspace: WorkspaceType) => void
  toggleSidebar: () => void
  setSidebarCollapsed: (collapsed: boolean) => void
  openRightPanel: (type: RightPanelContentType) => void
  closeRightPanel: () => void
  toggleRightPanel: (type: RightPanelContentType) => void
}

export const useLayoutStore = create<LayoutState>((set, get) => ({
  activeWorkspace: WorkspaceType.SUPPLY,
  sidebarCollapsed: false,
  isRightPanelOpen: false,
  rightPanelContentType: null,

  setActiveWorkspace: (workspace) => set({ activeWorkspace: workspace }),
  toggleSidebar: () => set((s) => ({ sidebarCollapsed: !s.sidebarCollapsed })),
  setSidebarCollapsed: (collapsed) => set({ sidebarCollapsed: collapsed }),
  openRightPanel: (type) => set({ isRightPanelOpen: true, rightPanelContentType: type }),
  closeRightPanel: () => set({ isRightPanelOpen: false, rightPanelContentType: null }),
  toggleRightPanel: (type) => {
    const { isRightPanelOpen, rightPanelContentType } = get()
    if (isRightPanelOpen && rightPanelContentType === type) {
      set({ isRightPanelOpen: false, rightPanelContentType: null })
    } else {
      set({ isRightPanelOpen: true, rightPanelContentType: type })
    }
  },
}))
