import { register } from '@/services/mock/registry'
import { endpoints } from '@/services/api/endpoints'
import { missionaryRequests } from '@/services/mock/fixtures/missionary-requests'
import { missionaryMemories } from '@/services/mock/fixtures/missionary-memories'
import {
  filterItems,
  findOrThrow,
  isoNow,
  makeId,
  paginate,
  updateById,
} from '@/services/mock/helpers'

const paginatedItems = (items, query) => {
  const { data, meta } = paginate(items, query)
  return { success: true, message: 'OK', data: { items: data, meta } }
}

register('GET', endpoints.listMissionaryRequests, ({ query }) =>
  paginatedItems(filterItems(missionaryRequests, query, { status: 'eq' }), query)
)

register('GET', endpoints.showMissionaryRequest, ({ params }) => ({
  success: true,
  message: 'OK',
  data: findOrThrow(missionaryRequests, params.id),
}))

register('PATCH', endpoints.changeMissionaryRequestStatus, ({ params, data }) => ({
  success: true,
  message: 'Status updated.',
  data: updateById(missionaryRequests, params.id, { status: data.status }),
}))

register('GET', endpoints.listMissionaryMemories, ({ query }) =>
  paginatedItems(missionaryMemories, query)
)

register('POST', endpoints.createMissionaryMemory, ({ data }) => {
  const memory = {
    id: makeId(),
    missionaryId: 8,
    title: data.title || '',
    description: data.description || '',
    missionaryRequestId: data.missionaryRequestId ?? null,
    media: (data.mediaIds ?? []).map((id) => ({
      id,
      collectionName: 'memory',
      fileName: `memory-${id}`,
      mimeType: 'image/png',
      fileSize: 0,
      url: '',
      downloadUrl: '',
    })),
    createdAt: isoNow(),
  }
  missionaryMemories.unshift(memory)
  return { success: true, message: 'Memory created.', data: memory }
})

register('PATCH', endpoints.updateMissionaryMemory, ({ params, data }) => {
  const memory = findOrThrow(missionaryMemories, params.id)
  if (data.title != null) memory.title = data.title
  if (data.description != null) memory.description = data.description
  const appended = (data.mediaIds ?? []).map((id) => ({
    id,
    collectionName: 'memory',
    fileName: `memory-${id}`,
    mimeType: 'image/png',
    fileSize: 0,
    url: '',
    downloadUrl: '',
  }))
  memory.media = [...(memory.media ?? []), ...appended]
  return { success: true, message: 'Memory updated.', data: memory }
})

register('DELETE', endpoints.deleteMissionaryMemory, ({ params }) => {
  const index = missionaryMemories.findIndex((m) => String(m.id) === String(params.id))
  if (index >= 0) missionaryMemories.splice(index, 1)
  return { success: true, message: 'Memory deleted.', data: null }
})

register('GET', endpoints.listAdminMissionaryRequests, ({ query }) =>
  paginatedItems(filterItems(missionaryRequests, query, { status: 'eq' }), query)
)

register('GET', endpoints.showAdminMissionaryRequest, ({ params }) => ({
  success: true,
  message: 'OK',
  data: findOrThrow(missionaryRequests, params.id),
}))

register('PATCH', endpoints.changeAdminMissionaryRequestStatus, ({ params, data }) => ({
  success: true,
  message: 'Status updated.',
  data: updateById(missionaryRequests, params.id, { status: data.status }),
}))
