import { renderHook, act, waitFor } from '@testing-library/react-native'
import { useMessages } from './use-messages'
import { root } from '@repo/core'
import { MessageController } 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('useMessages', () => {
  beforeEach(() => {
    jest.clearAllMocks()
  })

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

  describe('initial state', () => {
    it('should return initial state', () => {
      const mockMessageController = {
        list: jest.fn(),
      }
      ;(root.get as jest.Mock).mockReturnValue(mockMessageController)

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

      expect(result.current.data).toBeUndefined()
      expect(result.current.messages).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 messages successfully', async () => {
      const mockData = {
        messages: [
          { id: '1', content: 'Message 1', createdAt: new Date() },
          { id: '2', content: 'Message 2', createdAt: new Date() },
        ],
        total: 2,
        unreadCount: 0,
        page: 1,
        limit: 20,
        totalPages: 1,
      }

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

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

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

      expect(result.current.data).toEqual(mockData)
      expect(result.current.messages).toEqual(mockData.messages)
      expect(result.current.error).toBeNull()
      expect(result.current.loading).toBe(false)
    })

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

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

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

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

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

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

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

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

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

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

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

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

  describe('loadMore function', () => {
    it('should load more messages and append to existing', async () => {
      const mockFirstPage = {
        messages: [{ id: '1', content: 'Message 1', createdAt: new Date() }],
        total: 3,
        unreadCount: 1,
        page: 1,
        limit: 20,
        totalPages: 2,
      }

      const mockSecondPage = {
        messages: [{ id: '2', content: 'Message 2', createdAt: new Date() }],
        total: 3,
        unreadCount: 1,
        page: 2,
        limit: 20,
        totalPages: 2,
      }

      const mockMessageController = {
        list: jest.fn()
          .mockResolvedValueOnce(mockFirstPage)
          .mockResolvedValueOnce(mockSecondPage),
      }
      ;(root.get as jest.Mock).mockReturnValue(mockMessageController)

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

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

      expect(result.current.messages).toHaveLength(1)
      expect(result.current.hasMore).toBe(true)

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

      expect(result.current.messages).toHaveLength(2)
      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 mockMessageController = {
        list: jest.fn().mockReturnValue(fetchPromise),
      }
      ;(root.get as jest.Mock).mockReturnValue(mockMessageController)

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

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

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

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

      expect(mockMessageController.list).toHaveBeenCalledTimes(1)
    })

    it('should not load more if no more pages', async () => {
      const mockData = {
        messages: [{ id: '1', content: 'Message 1', createdAt: new Date() }],
        total: 1,
        unreadCount: 0,
        page: 1,
        limit: 20,
        totalPages: 1,
      }

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

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

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

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

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

      expect(mockMessageController.list).toHaveBeenCalledTimes(1)
    })
  })

  describe('refetch function', () => {
    it('should reset and reload messages', async () => {
      const mockFirstData = {
        messages: [{ id: '1', content: 'Message 1', createdAt: new Date() }],
        total: 2,
        unreadCount: 1,
        page: 1,
        limit: 20,
        totalPages: 2,
      }

      const mockSecondData = {
        messages: [{ id: '2', content: 'Message 2', createdAt: new Date() }],
        total: 2,
        unreadCount: 1,
        page: 1,
        limit: 20,
        totalPages: 2,
      }

      const mockMessageController = {
        list: jest.fn()
          .mockResolvedValueOnce(mockFirstData)
          .mockResolvedValueOnce(mockSecondData),
      }
      ;(root.get as jest.Mock).mockReturnValue(mockMessageController)

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

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

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

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

      expect(mockMessageController.list).toHaveBeenCalledTimes(2)
      expect(result.current.hasMore).toBe(true)
    })
  })
})
