import { describe, test, vi, expect, beforeEach } from 'vitest';

import { Client } from '@vulog/aima-client';

import { getServiceRegistrationOverview } from './getServiceRegistrationOverview';
import { UserServiceRegistrationOverview } from './types';

describe('getServiceRegistrationOverview', () => {
    const getMock = vi.fn();
    const client = {
        get: getMock,
        clientOptions: {
            fleetId: 'DLRSHP-FRANCE',
        },
    } as unknown as Client;

    const userId1 = 'f8043d72-8db8-48d7-896c-40744e2b549f';
    const userId2 = '9f0fd479-28a9-4754-9c77-611ea48bfb8f';

    beforeEach(() => {
        vi.clearAllMocks();
    });

    test('should throw on empty array', async () => {
        await expect(getServiceRegistrationOverview(client, [])).rejects.toThrow('Invalid args');
        expect(getMock).not.toHaveBeenCalled();
    });

    test('should throw on invalid UUID', async () => {
        await expect(getServiceRegistrationOverview(client, ['not-a-uuid'])).rejects.toThrow('Invalid args');
        expect(getMock).not.toHaveBeenCalled();
    });

    test('should call endpoint with single userId', async () => {
        const mockResponse: UserServiceRegistrationOverview[] = [
            {
                userId: userId1,
                catalog: [],
                profiles: [],
            },
        ];
        getMock.mockResolvedValueOnce({ data: mockResponse });

        const result = await getServiceRegistrationOverview(client, [userId1]);

        expect(getMock).toHaveBeenCalledWith(
            `boapi/proxy/user/fleets/DLRSHP-FRANCE/serviceRegistrationOverview?userId=${userId1}`
        );
        expect(result).toEqual(mockResponse);
    });

    test('should call endpoint with multiple userIds', async () => {
        getMock.mockResolvedValueOnce({ data: [] });

        await getServiceRegistrationOverview(client, [userId1, userId2]);

        const url = getMock.mock.calls[0][0] as string;
        expect(url).toContain(`userId=${userId1}`);
        expect(url).toContain(`userId=${userId2}`);
    });

    test('should propagate client errors', async () => {
        getMock.mockRejectedValueOnce(new Error('network down'));
        await expect(getServiceRegistrationOverview(client, [userId1])).rejects.toThrow('network down');
    });
});
