import { describe, it, expect, vi, beforeEach } from 'vitest'
import { financialService } from './financialService'
import { apiClient } from './apiClient'

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

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

    it('should fetch company data', async () => {
        const mockCompany = { id: 'company-1', corporate_name: 'Farmácia Central' }
        vi.mocked(apiClient.get).mockResolvedValue(mockCompany)

        const result = await financialService.getMyCompany()

        expect(apiClient.get).toHaveBeenCalledWith('/v1/companies/me')
        expect(result).toEqual(mockCompany)
    })

    it('should upload a document', async () => {
        const mockDocument = {
            id: 'doc-1',
            company_id: 'company-1',
            type: 'CNES',
            status: 'PENDING',
            url: 'https://example.com/doc',
            created_at: '2024-01-01T00:00:00Z'
        }

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

        const file = new File(['content'], 'doc.pdf', { type: 'application/pdf' })
        const result = await financialService.uploadDocument(file, 'CNES')

        expect(apiClient.post).toHaveBeenCalledWith('/v1/companies/documents', expect.any(FormData))
        expect(result).toEqual(mockDocument)
    })

    it('should list documents', async () => {
        const mockDocuments = {
            documents: [
                {
                    id: 'doc-1',
                    company_id: 'company-1',
                    type: 'CRF',
                    status: 'APPROVED',
                    url: 'https://example.com/doc',
                    created_at: '2024-01-01T00:00:00Z'
                }
            ]
        }

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

        const result = await financialService.listDocuments()

        expect(apiClient.get).toHaveBeenCalledWith('/v1/companies/documents')
        expect(result).toEqual(mockDocuments.documents)
    })

    it('should return balance', async () => {
        vi.mocked(apiClient.get).mockResolvedValue({ balance: 12500 })

        const result = await financialService.getBalance()

        expect(apiClient.get).toHaveBeenCalledWith('/v1/finance/balance')
        expect(result).toBe(12500)
    })

    it('should fetch ledger with pagination', async () => {
        const mockLedger = {
            entries: [
                {
                    id: 'entry-1',
                    company_id: 'company-1',
                    amount: 1000,
                    balance_after: 2000,
                    description: 'Venda',
                    type: 'CREDIT',
                    reference_id: 'ref-1',
                    created_at: '2024-01-01T00:00:00Z'
                }
            ],
            total: 1
        }

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

        const result = await financialService.getLedger(2, 50)

        expect(apiClient.get).toHaveBeenCalledWith('/v1/finance/ledger?page=2&limit=50')
        expect(result).toEqual(mockLedger)
    })

    it('should request withdrawal', async () => {
        const mockWithdrawal = {
            id: 'withdrawal-1',
            company_id: 'company-1',
            amount: 5000,
            status: 'PENDING',
            requested_at: '2024-01-02T00:00:00Z'
        }

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

        const result = await financialService.requestWithdrawal(5000)

        expect(apiClient.post).toHaveBeenCalledWith('/v1/finance/withdrawals', { amount: 5000 })
        expect(result).toEqual(mockWithdrawal)
    })
})
