import type { WalletTransactionResponseDto } from '@/api/generated/models' import axiosInstance from '@/config/axios' import { API_ROUTES } from '@/services/config' import { createServiceError, errorResult, handleServiceError, type ServiceCallOptions, type ServiceResult, shouldBubbleErrorToParent, successResult, } from '@/services/errorHandler' import { unwrapApiData, type ApiResponse } from '@/services/apiResponse' export type WalletTransaction = WalletTransactionResponseDto export interface AdminCreditWalletPayload { /** Amount in Toman (integer). */ amount: number description?: string } export interface AdminCreditWalletResult { walletBalance: number transaction: WalletTransaction } export const ADMIN_CREDIT_WALLET = async ( userId: string, payload: AdminCreditWalletPayload, options?: ServiceCallOptions ): Promise> => { try { const res = await axiosInstance.post>(API_ROUTES.USERS.ADMIN_WALLET_CREDIT(userId), payload) const body = res.data if (!body.success) { throw createServiceError(body.message || 'شارژ کیف‌پول ناموفق بود') } const data = unwrapApiData(body) if (data?.walletBalance === undefined || !data.transaction?.id) { throw createServiceError('پاسخ نامعتبر از سرور') } return successResult(data) } catch (error) { const normalizedError = handleServiceError(error, options) if (shouldBubbleErrorToParent(options)) { throw normalizedError } return errorResult(normalizedError) } }