import { describe, test, expect, vi, beforeEach, afterEach } from 'vitest';
import { Client } from '@vulog/aima-client';
import { getUserCreditsByEntityId } from './getUserCreditsByEntityId';

describe('getUserCreditsByEntityId', () => {
    const FLEET_ID = 'FLEET_ID';
    const ENTITY_ID = 'de66ea16-e942-4eaa-9f86-491e62bf59a2';
    const getMock = vi.fn();
    const client = {
        get: getMock,
        clientOptions: {
            fleetId: FLEET_ID,
        },
    } as unknown as Client;

    beforeEach(() => {
        getMock.mockReset();
        vi.useFakeTimers({ now: new Date('2025-01-12T13:35:50.123Z') });
    });

    afterEach(() => {
        vi.useRealTimers();
    });

    test('call OK', async () => {
        const data = {
            id: ENTITY_ID,
            credits: [
                {
                    id: 'credit-id',
                    name: 'Test Credit',
                    description: 'Test Description',
                    availableAmount: 100,
                    usedAmount: 50,
                    originId: 'origin-id',
                    entityId: ENTITY_ID,
                    creditAlreadyUsed: false,
                    updateDate: '2025-01-12T13:35:50.123Z',
                    type: 'LOCAL',
                },
            ],
        };

        getMock.mockResolvedValueOnce({ data });

        const result = await getUserCreditsByEntityId(client, ENTITY_ID);
        expect(result).toEqual(data);
        expect(getMock).toHaveBeenCalledWith(`/boapi/proxy/billing/fleets/${FLEET_ID}/wallet/entity/${ENTITY_ID}`);
    });

    test('invalid id', async () => {
        const rejects = expect(() => getUserCreditsByEntityId(client, 'invalid-id')).rejects;
        await rejects.toThrowError('Invalid id');
        await rejects.toSatisfy((error: any) => {
            expect(error).toHaveProperty('cause');
            expect(error.cause).toHaveLength(1);
            expect(error.cause[0]).toHaveProperty('code');
            expect(error.cause[0].code).toBe('invalid_string');
            expect(error.cause[0]).toHaveProperty('message');
            expect(error.cause[0].message).toBe('Invalid uuid');
            return true;
        });
    });
});
