import { describe, it, expect, vi, beforeEach } from 'vitest'
import { shippingService } from './shippingService'
import { apiClient } from './apiClient'
import type { ShippingSettings } from '../types/shipping'

// Mock apiClient
vi.mock('./apiClient', () => ({
    apiClient: {
        get: vi.fn(),
        post: vi.fn()
    }
}))

describe('shippingService', () => {
    beforeEach(() => {
        vi.clearAllMocks()
    })

    describe('getSettings', () => {
        it('should fetch shipping settings for a vendor', async () => {
            const mockSettings: ShippingSettings = {
                vendor_id: 'vendor-123',
                active: true,
                max_radius_km: 50,
                price_per_km_cents: 150,
                min_fee_cents: 1000,
                pickup_active: true,
                pickup_address: 'Rua Test, 123',
                pickup_hours: '08:00-18:00',
                latitude: -23.55,
                longitude: -46.63
            }

            vi.mocked(apiClient.get).mockResolvedValue(mockSettings)

            const result = await shippingService.getSettings('vendor-123')

            expect(apiClient.get).toHaveBeenCalledWith('/v1/shipping/settings/vendor-123')
            expect(result).toEqual(mockSettings)
            expect(result.active).toBe(true)
            expect(result.pickup_active).toBe(true)
        })

        it('should handle vendor not found', async () => {
            vi.mocked(apiClient.get).mockRejectedValue(new Error('Vendor not found'))

            await expect(shippingService.getSettings('invalid-vendor'))
                .rejects.toThrow('Vendor not found')
        })
    })

    describe('calculate', () => {
        it('should calculate shipping cost', async () => {
            const mockCalculation = {
                options: [
                    {
                        seller_id: 'seller-1',
                        delivery_fee_cents: 2500,
                        distance_km: 15.5,
                        estimated_days: 2,
                        pickup_available: true,
                        pickup_address: 'Rua Test, 123',
                        pickup_hours: '08:00-18:00'
                    }
                ]
            }

            vi.mocked(apiClient.post).mockResolvedValue(mockCalculation)

            const result = await shippingService.calculate({
                buyer_id: 'buyer-123',
                order_total_cents: 15000,
                items: [
                    { seller_id: 'seller-1', product_id: 'prod-1', quantity: 2, price_cents: 7500 }
                ]
            })

            expect(apiClient.post).toHaveBeenCalledWith('/v1/shipping/calculate', expect.objectContaining({
                buyer_id: 'buyer-123',
                order_total_cents: 15000
            }))
            expect(result.options).toHaveLength(1)
            expect(result.options[0].delivery_fee_cents).toBe(2500)
        })

        it('should handle API error', async () => {
            vi.mocked(apiClient.post).mockRejectedValue(new Error('Server error'))

            await expect(shippingService.calculate({
                buyer_id: 'buyer-123',
                order_total_cents: 15000,
                items: []
            })).rejects.toThrow('Server error')
        })
    })
})
