import { renderHook, act } from '@testing-library/react-native'
import { useUserLikes } from './use-user-likes'
import { root } from '@repo/core'
import { TemplateSocialController } from '@repo/sdk'
import { handleError } from './use-error'

jest.mock('@repo/core', () => ({
  root: {
    get: jest.fn(),
  },
}))

jest.mock('./use-error', () => ({
  handleError: jest.fn(async (cb) => {
    try {
      const data = await cb()
      return { data, error: null }
    } catch (e) {
      return { data: null, error: e }
    }
  }),
}))

describe('useUserLikes', () => {
  beforeEach(() => {
    jest.clearAllMocks()
  })

  afterEach(() => {
    jest.restoreAllMocks()
  })

  describe('initial state', () => {
    it('should return initial state with no data', () => {
      const { result } = renderHook(() => useUserLikes())

      expect(result.current.data).toBeUndefined()
      expect(result.current.likes).toEqual([])
      expect(result.current.loading).toBe(false)
      expect(result.current.loadingMore).toBe(false)
      expect(result.current.error).toBeNull()
      expect(result.current.hasMore).toBe(true)
    })
  })

  describe('execute function', () => {
    it('should load user likes successfully', async () => {
      const mockData = {
        likes: [
          {
            id: '1',
            templateId: 'template-1',
            createdAt: new Date('2024-01-01'),
            template: { id: 'template-1', name: 'Template 1' },
          },
          {
            id: '2',
            templateId: 'template-2',
            createdAt: new Date('2024-01-02'),
            template: { id: 'template-2', name: 'Template 2' },
          },
        ],
        total: 2,
        page: 1,
        limit: 20,
      }

      const mockController = {
        getUserLikes: jest.fn().mockResolvedValue(mockData),
      }
      ;(root.get as jest.Mock).mockReturnValue(mockController)

      const { result } = renderHook(() => useUserLikes())

      await act(async () => {
        await result.current.execute()
      })

      expect(mockController.getUserLikes).toHaveBeenCalledWith({
        limit: 20,
        page: 1,
      })
      expect(result.current.data).toEqual(mockData)
      expect(result.current.likes).toEqual(mockData.likes)
      expect(result.current.error).toBeNull()
      expect(result.current.hasMore).toBe(false)
    })

    it('should handle API errors', async () => {
      const mockError = {
        status: 500,
        statusText: 'Internal Server Error',
        message: 'Failed to load user likes',
      }

      const mockController = {
        getUserLikes: jest.fn().mockRejectedValue(mockError),
      }
      ;(root.get as jest.Mock).mockReturnValue(mockController)

      const { result } = renderHook(() => useUserLikes())

      await act(async () => {
        await result.current.execute()
      })

      expect(result.current.error).toEqual(mockError)
      expect(result.current.data).toBeUndefined()
    })

    it('should merge custom params with defaults', async () => {
      const mockData = {
        likes: [],
        total: 0,
        page: 2,
        limit: 10,
      }

      const mockController = {
        getUserLikes: jest.fn().mockResolvedValue(mockData),
      }
      ;(root.get as jest.Mock).mockReturnValue(mockController)

      const { result } = renderHook(() => useUserLikes())

      await act(async () => {
        await result.current.execute({ page: 2, limit: 10 })
      })

      expect(mockController.getUserLikes).toHaveBeenCalledWith({
        limit: 10,
        page: 2,
      })
    })

    it('should use initial params', async () => {
      const mockData = {
        likes: [],
        total: 0,
        page: 1,
        limit: 50,
      }

      const mockController = {
        getUserLikes: jest.fn().mockResolvedValue(mockData),
      }
      ;(root.get as jest.Mock).mockReturnValue(mockController)

      const { result } = renderHook(() => useUserLikes({ limit: 50 }))

      await act(async () => {
        await result.current.execute()
      })

      expect(mockController.getUserLikes).toHaveBeenCalledWith({
        limit: 50,
        page: 1,
      })
    })

    it('should support search parameter', async () => {
      const mockData = {
        likes: [],
        total: 0,
        page: 1,
        limit: 20,
      }

      const mockController = {
        getUserLikes: jest.fn().mockResolvedValue(mockData),
      }
      ;(root.get as jest.Mock).mockReturnValue(mockController)

      const { result } = renderHook(() => useUserLikes())

      await act(async () => {
        await result.current.execute({ search: 'test' })
      })

      expect(mockController.getUserLikes).toHaveBeenCalledWith({
        limit: 20,
        page: 1,
        search: 'test',
      })
    })
  })

  describe('loading state', () => {
    it('should set loading to true during fetch', async () => {
      let resolveFetch: (value: any) => void
      const fetchPromise = new Promise((resolve) => {
        resolveFetch = resolve
      })

      const mockController = {
        getUserLikes: jest.fn().mockReturnValue(fetchPromise),
      }
      ;(root.get as jest.Mock).mockReturnValue(mockController)

      const { result } = renderHook(() => useUserLikes())

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

      expect(result.current.loading).toBe(true)

      await act(async () => {
        resolveFetch!({ likes: [], total: 0, page: 1, limit: 20 })
        await fetchPromise
      })

      expect(result.current.loading).toBe(false)
    })

    it('should set loading to false after error', async () => {
      const mockError = { status: 500, message: 'Error' }

      const mockController = {
        getUserLikes: jest.fn().mockRejectedValue(mockError),
      }
      ;(root.get as jest.Mock).mockReturnValue(mockController)

      const { result } = renderHook(() => useUserLikes())

      await act(async () => {
        await result.current.execute()
      })

      expect(result.current.loading).toBe(false)
    })
  })

  describe('pagination - loadMore', () => {
    it('should load more likes and append to existing data', async () => {
      const page1Data = {
        likes: [
          {
            id: '1',
            templateId: 'template-1',
            createdAt: new Date('2024-01-01'),
          },
          {
            id: '2',
            templateId: 'template-2',
            createdAt: new Date('2024-01-02'),
          },
        ],
        total: 4,
        page: 1,
        limit: 2,
      }

      const page2Data = {
        likes: [
          {
            id: '3',
            templateId: 'template-3',
            createdAt: new Date('2024-01-03'),
          },
          {
            id: '4',
            templateId: 'template-4',
            createdAt: new Date('2024-01-04'),
          },
        ],
        total: 4,
        page: 2,
        limit: 2,
      }

      const mockController = {
        getUserLikes: jest.fn()
          .mockResolvedValueOnce(page1Data)
          .mockResolvedValueOnce(page2Data),
      }
      ;(root.get as jest.Mock).mockReturnValue(mockController)

      const { result } = renderHook(() => useUserLikes({ limit: 2 }))

      await act(async () => {
        await result.current.execute()
      })

      expect(result.current.likes).toHaveLength(2)
      expect(result.current.hasMore).toBe(true)

      await act(async () => {
        await result.current.loadMore()
      })

      expect(result.current.likes).toHaveLength(4)
      expect(result.current.likes).toEqual([
        ...page1Data.likes,
        ...page2Data.likes,
      ])
      expect(result.current.hasMore).toBe(false)
    })

    it('should not load more if already loading', async () => {
      let resolveFetch: (value: any) => void
      const fetchPromise = new Promise((resolve) => {
        resolveFetch = resolve
      })

      const mockController = {
        getUserLikes: jest.fn().mockReturnValue(fetchPromise),
      }
      ;(root.get as jest.Mock).mockReturnValue(mockController)

      const { result } = renderHook(() => useUserLikes())

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

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

      await act(async () => {
        resolveFetch!({ likes: [], total: 0, page: 1, limit: 20 })
        await fetchPromise
      })

      expect(mockController.getUserLikes).toHaveBeenCalledTimes(1)
    })

    it('should not load more if no more data', async () => {
      const mockData = {
        likes: [
          {
            id: '1',
            templateId: 'template-1',
            createdAt: new Date('2024-01-01'),
          },
        ],
        total: 1,
        page: 1,
        limit: 20,
      }

      const mockController = {
        getUserLikes: jest.fn().mockResolvedValue(mockData),
      }
      ;(root.get as jest.Mock).mockReturnValue(mockController)

      const { result } = renderHook(() => useUserLikes())

      await act(async () => {
        await result.current.execute()
      })

      expect(result.current.hasMore).toBe(false)

      await act(async () => {
        await result.current.loadMore()
      })

      expect(mockController.getUserLikes).toHaveBeenCalledTimes(1)
    })

    it('should set loadingMore state correctly', async () => {
      const page1Data = {
        likes: [
          {
            id: '1',
            templateId: 'template-1',
            createdAt: new Date('2024-01-01'),
          },
        ],
        total: 2,
        page: 1,
        limit: 1,
      }

      let resolveLoadMore: (value: any) => void
      const loadMorePromise = new Promise((resolve) => {
        resolveLoadMore = resolve
      })

      const mockController = {
        getUserLikes: jest.fn()
          .mockResolvedValueOnce(page1Data)
          .mockReturnValueOnce(loadMorePromise),
      }
      ;(root.get as jest.Mock).mockReturnValue(mockController)

      const { result } = renderHook(() => useUserLikes({ limit: 1 }))

      await act(async () => {
        await result.current.execute()
      })

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

      expect(result.current.loadingMore).toBe(true)

      await act(async () => {
        resolveLoadMore!({
          likes: [
            {
              id: '2',
              templateId: 'template-2',
              createdAt: new Date('2024-01-02'),
            },
          ],
          total: 2,
          page: 2,
          limit: 1,
        })
        await loadMorePromise
      })

      expect(result.current.loadingMore).toBe(false)
    })
  })

  describe('refetch function', () => {
    it('should reset and reload data from page 1', async () => {
      const initialData = {
        likes: [
          {
            id: '1',
            templateId: 'template-1',
            createdAt: new Date('2024-01-01'),
          },
        ],
        total: 1,
        page: 1,
        limit: 20,
      }

      const refreshedData = {
        likes: [
          {
            id: '1',
            templateId: 'template-1',
            createdAt: new Date('2024-01-01'),
          },
          {
            id: '2',
            templateId: 'template-2',
            createdAt: new Date('2024-01-02'),
          },
        ],
        total: 2,
        page: 1,
        limit: 20,
      }

      const mockController = {
        getUserLikes: jest.fn()
          .mockResolvedValueOnce(initialData)
          .mockResolvedValueOnce(refreshedData),
      }
      ;(root.get as jest.Mock).mockReturnValue(mockController)

      const { result } = renderHook(() => useUserLikes())

      await act(async () => {
        await result.current.execute()
      })

      expect(result.current.likes).toHaveLength(1)

      await act(async () => {
        await result.current.refetch()
      })

      expect(result.current.likes).toHaveLength(2)
      expect(mockController.getUserLikes).toHaveBeenCalledTimes(2)
    })

    it('should reset hasMore flag', async () => {
      const mockData = {
        likes: [
          {
            id: '1',
            templateId: 'template-1',
            createdAt: new Date('2024-01-01'),
          },
        ],
        total: 1,
        page: 1,
        limit: 20,
      }

      const mockController = {
        getUserLikes: jest.fn().mockResolvedValue(mockData),
      }
      ;(root.get as jest.Mock).mockReturnValue(mockController)

      const { result } = renderHook(() => useUserLikes())

      await act(async () => {
        await result.current.execute()
      })

      expect(result.current.hasMore).toBe(false)

      await act(async () => {
        await result.current.refetch()
      })

      expect(result.current.hasMore).toBe(false)
    })
  })

  describe('hasMore flag', () => {
    it('should calculate hasMore when more pages exist', async () => {
      const mockData = {
        likes: [
          {
            id: '1',
            templateId: 'template-1',
            createdAt: new Date('2024-01-01'),
          },
        ],
        total: 40,
        page: 1,
        limit: 20,
      }

      const mockController = {
        getUserLikes: jest.fn().mockResolvedValue(mockData),
      }
      ;(root.get as jest.Mock).mockReturnValue(mockController)

      const { result } = renderHook(() => useUserLikes())

      await act(async () => {
        await result.current.execute()
      })

      // total: 40, limit: 20 -> totalPages: 2
      // page: 1 < totalPages: 2 -> hasMore: true
      expect(result.current.hasMore).toBe(true)
    })

    it('should set hasMore to false on last page', async () => {
      const mockData = {
        likes: [
          {
            id: '1',
            templateId: 'template-1',
            createdAt: new Date('2024-01-01'),
          },
        ],
        total: 20,
        page: 1,
        limit: 20,
      }

      const mockController = {
        getUserLikes: jest.fn().mockResolvedValue(mockData),
      }
      ;(root.get as jest.Mock).mockReturnValue(mockController)

      const { result } = renderHook(() => useUserLikes())

      await act(async () => {
        await result.current.execute()
      })

      // total: 20, limit: 20 -> totalPages: 1
      // page: 1 >= totalPages: 1 -> hasMore: false
      expect(result.current.hasMore).toBe(false)
    })
  })

  describe('error handling', () => {
    it('should clear error on successful execute', async () => {
      const mockError = { status: 500, message: 'Error' }
      const mockData = {
        likes: [
          {
            id: '1',
            templateId: 'template-1',
            createdAt: new Date('2024-01-01'),
          },
        ],
        total: 1,
        page: 1,
        limit: 20,
      }

      const mockController = {
        getUserLikes: jest.fn()
          .mockRejectedValueOnce(mockError)
          .mockResolvedValueOnce(mockData),
      }
      ;(root.get as jest.Mock).mockReturnValue(mockController)

      const { result } = renderHook(() => useUserLikes())

      await act(async () => {
        await result.current.execute()
      })

      expect(result.current.error).toEqual(mockError)

      await act(async () => {
        await result.current.execute()
      })

      expect(result.current.error).toBeNull()
    })

    it('should handle loadMore errors without affecting existing data', async () => {
      const page1Data = {
        likes: [
          {
            id: '1',
            templateId: 'template-1',
            createdAt: new Date('2024-01-01'),
          },
        ],
        total: 2,
        page: 1,
        limit: 1,
      }

      const mockError = { status: 500, message: 'Error loading more' }

      const mockController = {
        getUserLikes: jest.fn()
          .mockResolvedValueOnce(page1Data)
          .mockRejectedValueOnce(mockError),
      }
      ;(root.get as jest.Mock).mockReturnValue(mockController)

      const { result } = renderHook(() => useUserLikes({ limit: 1 }))

      await act(async () => {
        await result.current.execute()
      })

      expect(result.current.likes).toHaveLength(1)

      await act(async () => {
        await result.current.loadMore()
      })

      expect(result.current.likes).toHaveLength(1)
      expect(result.current.loadingMore).toBe(false)
    })
  })
})
