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

interface GlobalState {
  // 主题设置
  theme: 'light' | 'dark' | 'auto'

  // 语言设置
  language: 'zh-CN' | 'en-US'

  // 侧边栏状态
  sidebarCollapsed: boolean

  // 全局加载状态
  globalLoading: boolean

  // 面包屑导航
  breadcrumbs: Array<{
    title: string
    path?: string
  }>

  // 通知消息
  notifications: Array<{
    id: string
    type: 'success' | 'error' | 'warning' | 'info'
    title: string
    message: string
    duration?: number
    timestamp: number
  }>

  // 当前选中的租户（如果是多租户系统）
  currentTenant: {
    id: string
    name: string
    code: string
  } | null
}

interface GlobalActions {
  // 主题操作
  setTheme: (theme: 'light' | 'dark' | 'auto') => void

  // 语言操作
  setLanguage: (language: 'zh-CN' | 'en-US') => void

  // 侧边栏操作
  toggleSidebar: () => void
  setSidebarCollapsed: (collapsed: boolean) => void

  // 全局加载状态
  setGlobalLoading: (loading: boolean) => void

  // 面包屑操作
  setBreadcrumbs: (breadcrumbs: Array<{ title: string; path?: string }>) => void
  addBreadcrumb: (breadcrumb: { title: string; path?: string }) => void

  // 通知操作
  addNotification: (notification: {
    type: 'success' | 'error' | 'warning' | 'info'
    title: string
    message: string
    duration?: number
  }) => void
  removeNotification: (id: string) => void
  clearNotifications: () => void

  // 租户操作
  setCurrentTenant: (tenant: { id: string; name: string; code: string } | null) => void
}

export const useGlobalStore = create<GlobalState & GlobalActions>()(
  persist(
    (set, get) => ({
      // 初始状态
      theme: 'light',
      language: 'zh-CN',
      sidebarCollapsed: false,
      globalLoading: false,
      breadcrumbs: [],
      notifications: [],
      currentTenant: null,

      // 主题操作
      setTheme: (theme) => set({ theme }),

      // 语言操作
      setLanguage: (language) => set({ language }),

      // 侧边栏操作
      toggleSidebar: () => set((state) => ({ sidebarCollapsed: !state.sidebarCollapsed })),
      setSidebarCollapsed: (collapsed) => set({ sidebarCollapsed: collapsed }),

      // 全局加载状态
      setGlobalLoading: (loading) => set({ globalLoading: loading }),

      // 面包屑操作
      setBreadcrumbs: (breadcrumbs) => set({ breadcrumbs }),
      addBreadcrumb: (breadcrumb) => set((state) => ({
        breadcrumbs: [...state.breadcrumbs, breadcrumb]
      })),

      // 通知操作
      addNotification: (notification) => {
        const id = Date.now().toString()
        const newNotification = {
          ...notification,
          id,
          timestamp: Date.now(),
          duration: notification.duration || 4500
        }

        set((state) => ({
          notifications: [...state.notifications, newNotification]
        }))

        // 自动移除通知
        if (newNotification.duration && newNotification.duration > 0) {
          setTimeout(() => {
            get().removeNotification(id)
          }, newNotification.duration)
        }
      },

      removeNotification: (id) => set((state) => ({
        notifications: state.notifications.filter(n => n.id !== id)
      })),

      clearNotifications: () => set({ notifications: [] }),

      // 租户操作
      setCurrentTenant: (tenant) => set({ currentTenant: tenant })
    }),
    {
      name: 'global-storage',
      partialize: (state) => ({
        theme: state.theme,
        language: state.language,
        sidebarCollapsed: state.sidebarCollapsed,
        currentTenant: state.currentTenant
      })
    }
  )
)