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

import { PersonalInformationUser, PersonalInformationUserType, personalInformationUserTypeSchema } from './types';

const argsSchema = z.object({
    ids: z.array(z.string().trim().min(1).uuid()).min(1),
    types: z.array(personalInformationUserTypeSchema).min(1),
});

export const getUsersPIByIds = async (
    client: Client,
    ids: string[],
    types: PersonalInformationUserType[]
): Promise<PersonalInformationUser[]> => {
    const result = argsSchema.safeParse({ ids, types });
    if (!result.success) {
        throw new TypeError('Invalid args', {
            cause: result.error.issues,
        });
    }

    return client
        .post<{ content: PersonalInformationUser[] }>(
            `/boapi/proxy/user/fleets/${client.clientOptions.fleetId}/users/pi/list?${result.data.types.map((type) => `types=${type}`).join('&')}`,
            {
                userIds: result.data.ids,
            }
        )
        .then(({ data }) => data.content);
};
