import {request} from "./request";

interface Code {
  image: string
}

export interface LoginData {
  access_token: string
  company: Company
  token: string
  user: User
  catch_key: string
}

interface CheckoutBody {
  code: string
  phone_number: string
  openid: string
}

interface CheckoutData {
  token: string
  phone_number: string
  user: User
  company: Company
}


interface LearningRecord {
  user_course_records: LearnRecord[]
}

interface CourseRecord {
  course: Curriculum
  data: Record<number, HourHistory>
}

interface HourCourse {
  courseHour: Record<number, Hour>,
  date: Record<number, { start: number, end: number }>
  duration: Record<number, number>
}

export interface StatisticsParam {
  start_time: number
  end_time: string
}

export const userApi = {
  login(code: string) {
    return request<LoginData>('/api/v1/auth/login/wechat', 'POST', {code})
  },
  checkout(data: CheckoutBody) {
    return request<CheckoutData>('/api/v1/auth/login/checkout', 'POST', data)
  },
  unbind(id: number) {
    return request(`/api/v1/auth/login/unbind/${id}`, "PUT")
  },
  putName(id: number, name: string) {
    return request<User>(`/api/v1/auth/login/${id}`, "PUT", {name})
  },
  code(catch_key: string) {
    return request<{ code: Code }>(`/api/v1/auth/login/code`, "POST", {openid: catch_key})
  },
  learningRecord(courseId?: number) {
    return request<LearningRecord>(`/api/v1/user/record/course`, "GET", courseId ? {courseId} : {})
  },
  /** 学习记录 */
  record() {
    return request<{ course: HourHistory[], durations: Record<number, number> }>(`/api/v1/category/history`, "GET")
  },
  courseRecord(course_id: string) {
    return request<CourseRecord>(`/api/v1/course/${course_id}/info`, "GET")
  },
  hourCourse(course_id: string, unique_ident: number) {
    return request<HourCourse>(`/api/v1/course/${course_id}/info/${unique_ident}`, "GET")
  },
  info(user_id: string) {
    return request<User>(`/api/v1/statistics/${user_id}`, "GET")
  },
  /**获取指定学员指定时间学习记录 */
  statistics(user_id: string | number, data: StatisticsParam) {
    return request<{
      data: Record<number, number>
    }>(`/api/v1/statistics/statistics/${user_id}?start_time=${data.start_time}&end_time=${data.end_time}`, "GET")
  },
  getCode(phone_number: number) {
    return request('/api/v1/sms/send?phone_number=' + phone_number, "GET")
  },
  /** 公司列表 */
  companyList() {
    return request<unknown[]>(`/api/v1/company/mine_list`, "GET")
  },
  companyReplace(id: number) {
    return request(`/api/v1/company/replace/${id}`, "PATCH")
  },
  // /** 上传头像 */
  // putAvatar(file: string) {
  //   return request('/api/v1/user/avatar', "PUT", {file})
  // }
}
