import type { UserGender } from '@/types' import { type AdminUserDetail } from '@/services/adminUserDetail' import { createServiceError, errorResult, handleServiceError, type ServiceCallOptions, type ServiceResult, shouldBubbleErrorToParent, successResult, } from '@/services/errorHandler' import { unwrapApiData } from '@/services/apiResponse' import { getAdminUsers } from '@/api/generated/admin-users/admin-users' import { getAdminUserDetail } from '@/api/generated/admin-user-detail/admin-user-detail' import type { UserProfileResponseDto } from '@/api/generated/models' /** Subset of AdminUpdateUserDto — only changed fields should be sent (partial PATCH). */ export type AdminUpdateUserPayload = Partial<{ firstName: string lastName: string gender: UserGender cityId: number avatarUrl: string bio: string status: 'active' | 'suspended' hostPlan: 'free' | 'unlimited' }> const adminUsersApi = getAdminUsers() const adminUserDetailApi = getAdminUserDetail() export const ADMIN_UPDATE_USER = async ( userId: string, payload: AdminUpdateUserPayload, options?: ServiceCallOptions ): Promise> => { try { const res = await adminUsersApi.adminUsersControllerUpdate(userId, payload) const body = res.data if (!body.success) { throw createServiceError(body.message || 'ویرایش اطلاعات کاربر ناموفق بود') } const updated = unwrapApiData(body) if (!updated?.id) { throw createServiceError('پاسخ نامعتبر از سرور') } const detailResponse = await adminUserDetailApi.adminUserDetailControllerGetDetail(userId) if (!detailResponse.data.success) { throw createServiceError(detailResponse.data.message || 'دریافت اطلاعات به‌روزشده ناموفق بود') } const detail = unwrapApiData(detailResponse.data) if (!detail?.id) throw createServiceError('پاسخ نامعتبر از سرور') return successResult(detail) } catch (error) { const normalizedError = handleServiceError(error, options) if (shouldBubbleErrorToParent(options)) { throw normalizedError } return errorResult(normalizedError) } } export const ADMIN_CHANGE_USER_MOBILE = async ( userId: string, mobile: string, options?: ServiceCallOptions ): Promise> => { try { const res = await adminUsersApi.adminUsersControllerChangeMobile(userId, { mobile }) const body = res.data if (!body.success) { throw createServiceError(body.message || 'تغییر شماره موبایل کاربر ناموفق بود') } const updated = unwrapApiData(body) if (!updated?.id || updated.mobile !== mobile) { throw createServiceError('پاسخ نامعتبر از سرور') } return successResult(updated) } catch (error) { const normalizedError = handleServiceError(error, options) if (shouldBubbleErrorToParent(options)) throw normalizedError return errorResult(normalizedError) } }