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

const paths = ['/availableAmount', '/percentage'] as const;
export type Paths = (typeof paths)[number];

const schema = z.object({
    walletId: z.string().trim().min(1).uuid(),
    actions: z
        .array(
            z.object({
                op: z.enum(['replace']),
                path: z.enum(paths),
                value: z.string().min(1),
            })
        )
        .min(1)
        .max(1),
});

export const updateWallet = async (client: Client, walletId: string, actions: PatchAction<Paths>[]): Promise<void> => {
    const result = schema.safeParse({ walletId, actions });
    if (!result.success) {
        throw new TypeError('Invalid args', {
            cause: result.error.issues,
        });
    }

    await client.patch(`/boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/wallets/${walletId}`, actions, {
        headers: {
            'Content-Type': 'application/json-patch+json',
        },
    });
};
