import { renderHook, act } from '@testing-library/react-native'
import { useTemplateSocialStore } from './templateSocialStore'

describe('useTemplateSocialStore', () => {
  beforeEach(() => {
    // 每个测试前清空 store 状态
    const { result } = renderHook(() => useTemplateSocialStore())
    act(() => {
      result.current.clear()
    })
  })

  describe('initial state', () => {
    it('should have empty likedMap initially', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      expect(result.current.likedMap).toEqual({})
    })

    it('should have empty favoritedMap initially', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      expect(result.current.favoritedMap).toEqual({})
    })
  })

  describe('setLiked', () => {
    it('should set liked status for a template', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      act(() => {
        result.current.setLiked('template-1', true)
      })

      expect(result.current.likedMap['template-1']).toBe(true)
    })

    it('should override existing liked status', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      act(() => {
        result.current.setLiked('template-1', true)
      })

      act(() => {
        result.current.setLiked('template-1', false)
      })

      expect(result.current.likedMap['template-1']).toBe(false)
    })

    it('should not affect other templates when setting one', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      act(() => {
        result.current.setLiked('template-1', true)
        result.current.setLiked('template-2', false)
      })

      expect(result.current.likedMap['template-1']).toBe(true)
      expect(result.current.likedMap['template-2']).toBe(false)
    })
  })

  describe('setFavorited', () => {
    it('should set favorited status for a template', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      act(() => {
        result.current.setFavorited('template-1', true)
      })

      expect(result.current.favoritedMap['template-1']).toBe(true)
    })

    it('should override existing favorited status', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      act(() => {
        result.current.setFavorited('template-1', true)
      })

      act(() => {
        result.current.setFavorited('template-1', false)
      })

      expect(result.current.favoritedMap['template-1']).toBe(false)
    })

    it('should not affect other templates when setting one', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      act(() => {
        result.current.setFavorited('template-1', true)
        result.current.setFavorited('template-2', false)
      })

      expect(result.current.favoritedMap['template-1']).toBe(true)
      expect(result.current.favoritedMap['template-2']).toBe(false)
    })
  })

  describe('setLikedStates', () => {
    it('should batch set liked states', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      act(() => {
        result.current.setLikedStates({
          'template-1': true,
          'template-2': false,
          'template-3': true,
        })
      })

      expect(result.current.likedMap['template-1']).toBe(true)
      expect(result.current.likedMap['template-2']).toBe(false)
      expect(result.current.likedMap['template-3']).toBe(true)
    })

    it('should merge with existing liked states', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      act(() => {
        result.current.setLiked('template-1', true)
      })

      act(() => {
        result.current.setLikedStates({
          'template-2': true,
          'template-3': false,
        })
      })

      expect(result.current.likedMap['template-1']).toBe(true)
      expect(result.current.likedMap['template-2']).toBe(true)
      expect(result.current.likedMap['template-3']).toBe(false)
    })

    it('should override existing states with batch update', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      act(() => {
        result.current.setLiked('template-1', true)
      })

      act(() => {
        result.current.setLikedStates({
          'template-1': false,
          'template-2': true,
        })
      })

      expect(result.current.likedMap['template-1']).toBe(false)
      expect(result.current.likedMap['template-2']).toBe(true)
    })
  })

  describe('setFavoritedStates', () => {
    it('should batch set favorited states', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      act(() => {
        result.current.setFavoritedStates({
          'template-1': true,
          'template-2': false,
          'template-3': true,
        })
      })

      expect(result.current.favoritedMap['template-1']).toBe(true)
      expect(result.current.favoritedMap['template-2']).toBe(false)
      expect(result.current.favoritedMap['template-3']).toBe(true)
    })

    it('should merge with existing favorited states', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      act(() => {
        result.current.setFavorited('template-1', true)
      })

      act(() => {
        result.current.setFavoritedStates({
          'template-2': true,
          'template-3': false,
        })
      })

      expect(result.current.favoritedMap['template-1']).toBe(true)
      expect(result.current.favoritedMap['template-2']).toBe(true)
      expect(result.current.favoritedMap['template-3']).toBe(false)
    })

    it('should override existing states with batch update', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      act(() => {
        result.current.setFavorited('template-1', true)
      })

      act(() => {
        result.current.setFavoritedStates({
          'template-1': false,
          'template-2': true,
        })
      })

      expect(result.current.favoritedMap['template-1']).toBe(false)
      expect(result.current.favoritedMap['template-2']).toBe(true)
    })
  })

  describe('isLiked', () => {
    it('should return true for liked template', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      act(() => {
        result.current.setLiked('template-1', true)
      })

      expect(result.current.isLiked('template-1')).toBe(true)
    })

    it('should return false for unliked template', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      act(() => {
        result.current.setLiked('template-1', false)
      })

      expect(result.current.isLiked('template-1')).toBe(false)
    })

    it('should return false for non-existent template', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      expect(result.current.isLiked('template-1')).toBe(false)
    })
  })

  describe('isFavorited', () => {
    it('should return true for favorited template', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      act(() => {
        result.current.setFavorited('template-1', true)
      })

      expect(result.current.isFavorited('template-1')).toBe(true)
    })

    it('should return false for unfavorited template', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      act(() => {
        result.current.setFavorited('template-1', false)
      })

      expect(result.current.isFavorited('template-1')).toBe(false)
    })

    it('should return false for non-existent template', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      expect(result.current.isFavorited('template-1')).toBe(false)
    })
  })

  describe('clear', () => {
    it('should clear all likedMap and favoritedMap', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      act(() => {
        result.current.setLikedStates({
          'template-1': true,
          'template-2': false,
        })
        result.current.setFavoritedStates({
          'template-1': true,
          'template-3': false,
        })
      })

      expect(result.current.likedMap['template-1']).toBe(true)
      expect(result.current.favoritedMap['template-1']).toBe(true)

      act(() => {
        result.current.clear()
      })

      expect(result.current.likedMap).toEqual({})
      expect(result.current.favoritedMap).toEqual({})
    })

    it('should allow setting states after clear', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      act(() => {
        result.current.setLiked('template-1', true)
      })

      act(() => {
        result.current.clear()
      })

      act(() => {
        result.current.setLiked('template-2', true)
      })

      expect(result.current.likedMap['template-2']).toBe(true)
      expect(result.current.likedMap['template-1']).toBeUndefined()
    })
  })

  describe('integration tests', () => {
    it('should handle mixed liked and favorited states', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      act(() => {
        result.current.setLiked('template-1', true)
        result.current.setFavorited('template-1', false)
        result.current.setLiked('template-2', false)
        result.current.setFavorited('template-2', true)
      })

      expect(result.current.isLiked('template-1')).toBe(true)
      expect(result.current.isFavorited('template-1')).toBe(false)
      expect(result.current.isLiked('template-2')).toBe(false)
      expect(result.current.isFavorited('template-2')).toBe(true)
    })

    it('should maintain independence between likedMap and favoritedMap', () => {
      const { result } = renderHook(() => useTemplateSocialStore())

      act(() => {
        result.current.setLiked('template-1', true)
      })

      act(() => {
        result.current.setFavorited('template-1', true)
      })

      act(() => {
        result.current.clear()
      })

      // clear 应该清空两个 map
      expect(result.current.isLiked('template-1')).toBe(false)
      expect(result.current.isFavorited('template-1')).toBe(false)
    })
  })
})
