import type { CompleteProfileDto, UserProfileResponseDto } from '@/api/generated/models' import { createServiceError, errorResult, handleServiceError, type ServiceCallOptions, type ServiceResult, shouldBubbleErrorToParent, successResult, } from '@/services/errorHandler' import { unwrapApiData } from '@/services/apiResponse' import { ANALYTICS_EVENTS, trackAnalyticsEventOnce } from '@/lib/analytics' import { getUsers } from '@/api/generated/users/users' import { texts } from '@/texts' export type UserProfile = UserProfileResponseDto export type CompleteProfilePayload = CompleteProfileDto const usersApi = getUsers() /** Completes first-time admin profile after OTP registration. */ export const COMPLETE_PROFILE = async (data: CompleteProfilePayload, options?: ServiceCallOptions): Promise> => { try { const res = await usersApi.usersControllerCompleteProfile({ firstName: data.firstName.trim(), lastName: data.lastName.trim(), gender: data.gender, dateOfBirth: data.dateOfBirth, cityId: data.cityId, }) const payload = res.data if (!payload.success) { throw createServiceError(payload.message || texts.profile.completeProfileFailed) } const profile = unwrapApiData(payload) if (!profile?.id) { throw createServiceError(texts.common.invalidServerResponse) } trackAnalyticsEventOnce(ANALYTICS_EVENTS.SIGN_UP, profile.id, { method: 'otp_profile_completion' }) return successResult(profile) } catch (error) { const normalizedError = handleServiceError(error, options) if (shouldBubbleErrorToParent(options)) { throw normalizedError } return errorResult(normalizedError) } }