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

import {
    PersonalInformationProfile,
    PersonalInformationProfileType,
    personalInformationProfileTypeSchema,
} from './types';

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

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

    return client
        .get<PersonalInformationProfile>(
            `/boapi/proxy/user/fleets/${client.clientOptions.fleetId}/users/${result.data.userId}/profiles/${result.data.profileId}/pi?types=${result.data.types.join(',')}`
        )
        .then(({ data }) => data);
};
