import qs from "qs"
import useSWR from "swr"
import { fetchApi } from "./base"

export interface IQuiz {
  id: string
  program_id: string
  sequence: number
  question: string
  quiz_type: string
  choice?: string[]
  choice1?: string
  choice2?: string
  choice3?: string
  choice4?: string
  answer?: string[]
  answer1?: string
  answer2?: string
  answer3?: string
  answer4?: string
  hint: string
  comment: string
  status: string
  updated_at: Date
  created_at: Date
}

export interface QuizFormValue extends Omit<IQuiz, "id" | "created_at" | "updated_at" | "status"> {}

interface QuizParams {
  id?: any
  program_id?: string
  page?: number
  limit?: number
}

export interface QuizResponse {
  message: string
  data: any
}

export interface QuizzesResponse {
  message: string
  data: IQuiz[]
  total: number
  page: number
  totalPage: number
  pageSize: number
}

export const useQuizzes = (params: QuizParams = {}) => {
  const url = `api/quiz?${qs.stringify(params)}`
  return useSWR<QuizzesResponse>(url)
}

export const useQuiz = (params: QuizParams = {}) => {
  const id = params.id ? (params.id as string) : ""
  const url = `api/quiz/${id}`
  return useSWR<QuizResponse>(url)
}

export const create = (value: QuizFormValue) => {
  console.log("create", value)
  return fetchApi.post(`api/quiz`, { body: JSON.stringify(value) })
}

export const update = (id: string, value: QuizFormValue) => {
  return fetchApi.put(`api/quiz/${id}`, { body: JSON.stringify(value) })
}
