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

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

import { requestServiceRegistration } from './requestServiceRegistration';

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

    const profileId = '21204139-0f32-410f-839f-c7fb005aaf3e';
    const serviceId = 'f1e8b653-a4a3-4950-8e3a-768b1f1b5639';

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

    test('should throw on invalid profileId', async () => {
        await expect(requestServiceRegistration(client, 'not-a-uuid', serviceId)).rejects.toThrow('Invalid args');
        expect(putMock).not.toHaveBeenCalled();
    });

    test('should throw on invalid serviceId', async () => {
        await expect(requestServiceRegistration(client, profileId, 'not-a-uuid')).rejects.toThrow('Invalid args');
        expect(putMock).not.toHaveBeenCalled();
    });

    test('should call PUT with correct URL and return response', async () => {
        putMock.mockResolvedValueOnce({ data: 'OK' });

        const result = await requestServiceRegistration(client, profileId, serviceId);

        expect(putMock).toHaveBeenCalledWith(
            `boapi/proxy/user/fleets/DLRSHP-FRANCE/profiles/${profileId}/services/${serviceId}`
        );
        expect(result).toBe('OK');
    });

    test('should propagate client errors', async () => {
        putMock.mockRejectedValueOnce(new Error('network down'));
        await expect(requestServiceRegistration(client, profileId, serviceId)).rejects.toThrow('network down');
    });
});
