import { register } from '@/services/mock/registry'
import { endpoints } from '@/services/api/endpoints'
import { studentHomeworks } from '@/services/mock/fixtures/student-homeworks'
import { filterItems, findOrThrow, isoNow, makeId, paginate } from '@/services/mock/helpers'

register('GET', endpoints.getStudentHomeworks, ({ query }) => {
  const list = filterItems(studentHomeworks, query, {
    sessionId: (item, v) => String(item.sessionId) === String(v),
    courseId: (item, v) => String(item.session?.course?.id) === String(v),
    termId: (item, v) => String(item.session?.course?.term?.id) === String(v),
  })
  const { data: items, meta } = paginate(list, query)
  return { success: true, message: 'OK', data: { items, meta } }
})

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

register('POST', endpoints.submitHomework, ({ params, data }) => ({
  success: true,
  message: 'Homework submitted.',
  data: {
    id: makeId(),
    homeworkId: Number(params.homeworkId),
    userId: 5,
    status: 'pending',
    teacherFeedback: null,
    reviewedAt: null,
    media: [
      {
        id: Number(data.mediaId) || makeId(),
        collectionName: 'submission',
        fileName: 'submission',
        url: null,
        downloadUrl: null,
      },
    ],
    submittedAt: isoNow(),
  },
}))
