import { setActivePinia, createPinia } from 'pinia'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { useItemStore } from '../itemStore'
import type { Item } from '@/types/item'
import { API_ENDPOINTS } from '@/config/api-config'

// Mock the apiClient used inside the store
vi.mock('@/services/api', () => ({
    apiClient: {
        get: vi.fn(),
        post: vi.fn(),
        put: vi.fn(),
        delete: vi.fn(),
    },
    API_ENDPOINTS,
}))

import { apiClient } from '@/services/api'

const mockApi = apiClient as unknown as {
    get: ReturnType<typeof vi.fn>
    post: ReturnType<typeof vi.fn>
    put: ReturnType<typeof vi.fn>
    delete: ReturnType<typeof vi.fn>
}

describe('itemStore', () => {
    beforeEach(() => {
        setActivePinia(createPinia())
        vi.clearAllMocks()
    })

    it('fetchItems populates state', async () => {
        const listId = 1
        const mockItems: Item[] = [
            {
                id: 10,
                name: 'Milk',
                quantity: 1,
                is_complete: false,
                list_id: listId,
                created_at: '',
                updated_at: '',
                version: 1,
            },
        ]

        mockApi.get.mockResolvedValue({ data: mockItems })

        const store = useItemStore()
        await store.fetchItems(listId)

        expect(mockApi.get).toHaveBeenCalledWith(API_ENDPOINTS.LISTS.ITEMS(String(listId)))
        expect(store.itemsByList[listId]).toEqual(mockItems)
    })
}) 