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

interface TemplateSocialState {
  // 点赞状态缓存: templateId -> boolean
  likedMap: Record<string, boolean>

  // 收藏状态缓存: templateId -> boolean
  favoritedMap: Record<string, boolean>

  // 点赞数量缓存: templateId -> number
  likeCountMap: Record<string, number>

  // 收藏数量缓存: templateId -> number
  favoriteCountMap: Record<string, number>

  // 批量更新点赞状态
  setLikedStates: (states: Record<string, boolean>) => void

  // 批量更新收藏状态
  setFavoritedStates: (states: Record<string, boolean>) => void

  // 批量更新点赞数量
  setLikeCountStates: (states: Record<string, number>) => void

  // 批量更新收藏数量
  setFavoriteCountStates: (states: Record<string, number>) => void

  // 更新单个点赞状态
  setLiked: (templateId: string, liked: boolean) => void

  // 更新单个收藏状态
  setFavorited: (templateId: string, favorited: boolean) => void

  // 更新单个点赞数量
  setLikeCount: (templateId: string, count: number) => void

  // 更新单个收藏数量
  setFavoriteCount: (templateId: string, count: number) => void

  // 增加点赞数量（乐观更新）
  incrementLikeCount: (templateId: string) => void

  // 减少点赞数量（乐观更新）
  decrementLikeCount: (templateId: string) => void

  // 获取点赞状态（如果不存在返回 undefined）
  getLiked: (templateId: string) => boolean | undefined

  // 获取收藏状态（如果不存在返回 undefined）
  getFavorited: (templateId: string) => boolean | undefined

  // 获取点赞数量（如果不存在返回 undefined）
  getLikeCount: (templateId: string) => number | undefined

  // 获取收藏数量（如果不存在返回 undefined）
  getFavoriteCount: (templateId: string) => number | undefined

  // 获取点赞状态（如果不存在返回 false）
  isLiked: (templateId: string) => boolean

  // 获取收藏状态（如果不存在返回 false）
  isFavorited: (templateId: string) => boolean

  // 清空所有缓存
  clear: () => void
}

export const useTemplateSocialStore = create<TemplateSocialState>()(
  devtools(
    (set, get) => ({
      // State
      likedMap: {},
      favoritedMap: {},
      likeCountMap: {},
      favoriteCountMap: {},

      // Actions
      setLikedStates: (states) => {
        set((state) => ({
          likedMap: { ...state.likedMap, ...states },
        }))
      },

      setFavoritedStates: (states) => {
        set((state) => ({
          favoritedMap: { ...state.favoritedMap, ...states },
        }))
      },

      setLikeCountStates: (states) => {
        set((state) => ({
          likeCountMap: { ...state.likeCountMap, ...states },
        }))
      },

      setFavoriteCountStates: (states) => {
        set((state) => ({
          favoriteCountMap: { ...state.favoriteCountMap, ...states },
        }))
      },

      setLiked: (templateId, liked) => {
        set((state) => ({
          likedMap: { ...state.likedMap, [templateId]: liked },
        }))
      },

      setFavorited: (templateId, favorited) => {
        set((state) => ({
          favoritedMap: { ...state.favoritedMap, [templateId]: favorited },
        }))
      },

      setLikeCount: (templateId, count) => {
        set((state) => ({
          likeCountMap: { ...state.likeCountMap, [templateId]: count },
        }))
      },

      setFavoriteCount: (templateId, count) => {
        set((state) => ({
          favoriteCountMap: { ...state.favoriteCountMap, [templateId]: count },
        }))
      },

      incrementLikeCount: (templateId) => {
        set((state) => ({
          likeCountMap: {
            ...state.likeCountMap,
            [templateId]: (state.likeCountMap[templateId] || 0) + 1,
          },
        }))
      },

      decrementLikeCount: (templateId) => {
        set((state) => {
          const currentCount = state.likeCountMap[templateId] || 0
          return {
            likeCountMap: {
              ...state.likeCountMap,
              [templateId]: Math.max(0, currentCount - 1),
            },
          }
        })
      },

      getLiked: (templateId) => {
        return get().likedMap[templateId]
      },

      getFavorited: (templateId) => {
        return get().favoritedMap[templateId]
      },

      getLikeCount: (templateId) => {
        return get().likeCountMap[templateId]
      },

      getFavoriteCount: (templateId) => {
        return get().favoriteCountMap[templateId]
      },

      isLiked: (templateId) => {
        return get().likedMap[templateId] || false
      },

      isFavorited: (templateId) => {
        return get().favoritedMap[templateId] || false
      },

      clear: () => {
        set({
          likedMap: {},
          favoritedMap: {},
          likeCountMap: {},
          favoriteCountMap: {},
        })
      },
    }),
    { name: 'TemplateSocialStore' }
  )
)

// 选择器 hook：只订阅特定模板的状态
// 当该模板的状态变化时才会触发重新渲染
export function useTemplateLiked(templateId: string | undefined) {
  return useTemplateSocialStore((state) =>
    templateId ? state.likedMap[templateId] : undefined
  )
}

export function useTemplateFavorited(templateId: string | undefined) {
  return useTemplateSocialStore((state) =>
    templateId ? state.favoritedMap[templateId] : undefined
  )
}

export function useTemplateLikeCount(templateId: string | undefined) {
  return useTemplateSocialStore((state) =>
    templateId ? state.likeCountMap[templateId] : undefined
  )
}

export function useTemplateFavoriteCount(templateId: string | undefined) {
  return useTemplateSocialStore((state) =>
    templateId ? state.favoriteCountMap[templateId] : undefined
  )
}

// 为了方便使用，导出 templateSocialStore 别名
export const templateSocialStore = useTemplateSocialStore
