import { register } from '@/services/mock/registry'
import { endpoints } from '@/services/api/endpoints'
import { pendingStudents } from '@/services/mock/fixtures/admin-pending-students'
import {
  filterDateRange,
  filterItems,
  findOrThrow,
  paginate,
  removeById,
  updateById,
} from '@/services/mock/helpers'

register('GET', endpoints.getPendingStudents, ({ query }) => {
  let list = filterItems(pendingStudents, query, {
    name: (item, v) =>
      `${item.firstName} ${item.lastName}`.toLowerCase().includes(String(v).toLowerCase()),
    nationalCode: 'includes',
  })
  list = filterDateRange(list, query, 'submittedAt')
  return paginate(list, query)
})

register('GET', endpoints.showPendingStudent, ({ params }) => ({
  data: findOrThrow(pendingStudents, params.id),
}))

register('POST', endpoints.changeStatusPendingStudent, ({ params, data }) => {
  const target = findOrThrow(pendingStudents, params.id)
  const patch = {
    status: data.status,
    verification: {
      ...target.verification,
      status: data.status,
      rejectionReason: data.rejectionReason || '',
      adminNote: data.adminNote || '',
    },
  }
  updateById(pendingStudents, params.id, patch)
  if (data.status === 'approved') {
    removeById(pendingStudents, params.id)
  }
  return { data: { user: { ...target, ...patch } } }
})

register('GET', endpoints.downloadPdfPendingStudentInfo, ({ params }) => ({
  data: {
    downloadUrl: `https://example.com/mock-pdf-${params.id}.pdf`,
  },
}))
