import { makeId } from '@/services/mock/helpers'
import { register } from '@/services/mock/registry'
import { endpoints } from '@/services/api/endpoints'
import { currentMe } from '@/services/mock/fixtures/me'

const fakeToken = 'mock-token-1234567890'
let registerData = {}

register('POST', endpoints.login, () => ({
  data: { user: currentMe, token: fakeToken },
}))

register('POST', endpoints.requestLoginOtp, () => ({
  success: true,
  message: 'If the phone is registered, a code has been sent.',
  data: null,
}))

register('POST', endpoints.verifyLoginOtp, () => ({
  data: { user: currentMe, token: fakeToken },
}))

register('POST', endpoints.verifyCode, () => ({
  data: { user: currentMe, token: fakeToken },
}))

register('POST', endpoints.register, ({ data }) => ({
  data: { user: { ...currentMe, ...data }, token: fakeToken },
}))

register('POST', endpoints.forgotPassword, () => ({
  success: true,
  message: 'If the phone is registered, a code has been sent.',
  data: null,
}))

register('POST', endpoints.verifyForgotPasswordCode, () => ({
  data: { reset_token: fakeToken },
}))

register('POST', endpoints.resetPassword, () => ({
  success: true,
  message: 'Password updated.',
  data: null,
}))

register('POST', endpoints.logout, () => ({ data: { message: 'خروج موفق' } }))

register('GET', endpoints.me, () => ({
  success: true,
  message: 'OK',
  data: currentMe,
}))

register('PATCH', endpoints.updateProfile, ({ data }) => {
  Object.assign(currentMe, {
    name: data.name ?? currentMe.name,
    email: data.email ?? currentMe.email,
    phone: data.phone ?? currentMe.phone,
    firstName: data.firstName ?? currentMe.firstName,
    lastName: data.lastName ?? currentMe.lastName,
    phoneNumber: data.phoneNumber ?? currentMe.phoneNumber,
    nationalCode: data.nationalCode ?? currentMe.nationalCode,
  })
  if (data.provinceId) {
    currentMe.address = currentMe.address || {}
    currentMe.address.province = { id: data.provinceId, name: '' }
  }
  if (data.cityId) {
    currentMe.address = currentMe.address || {}
    currentMe.address.city = { id: data.cityId, name: '' }
  }
  if (data.address) {
    currentMe.address = { ...currentMe.address, address: data.address }
  }
  currentMe.profile = {
    ...currentMe.profile,
    bio: data.bio ?? currentMe.profile?.bio,
    birthDate: data.birthDate ?? currentMe.profile?.birthDate,
    maritalStatus: data.maritalStatus ?? currentMe.profile?.maritalStatus,
    gender: data.gender ?? currentMe.profile?.gender,
  }
  return { data: currentMe }
})

register('POST', endpoints.completeProfile, ({ data }) => ({
  data: { user: { ...currentMe, ...data } },
}))

register('GET', endpoints.registerData, () => ({ data: registerData }))

register('POST', endpoints.registerData, ({ data }) => {
  registerData = { ...registerData, ...data?.data }
  return { data: registerData }
})

register('GET', endpoints.getRegistrationQuestionVideo, () => ({
  data: { url: '', id: 0 },
}))

register('POST', endpoints.uploadMedia, () => {
  const id = makeId()
  return {
    success: true,
    message: 'Media uploaded.',
    data: {
      id,
      uploadId: id,
      url: `https://picsum.photos/seed/${id}/240/240`,
    },
  }
})
