import { defHttp } from '#/api/request';

const prefix = '/api/tms/practice';

async function unwrapData<T>(promise: Promise<any>): Promise<T> {
  const res = await promise;
  if (res && typeof res === 'object' && 'data' in res && ('code' in res || 'msg' in res)) {
    return res.data as T;
  }
  return res as T;
}

export interface PracticeItem {
  id?: string;
  userId?: string;
  userName?: string;
  deptName?: string;
  content?: string;
  score?: number;
  resultFlag?: string;
}

export interface PracticeForm {
  id?: string;
  taskId?: string;
  recordId?: string;
  teacherId?: string;
  practiceTime?: string;
  remark?: string;
  items?: PracticeItem[];
}

export function getPracticeByRecord(recordId: string) {
  return unwrapData<PracticeForm>(defHttp.get({ url: `${prefix}/record/${recordId}` }));
}

export function savePractice(data: PracticeForm) {
  return unwrapData<string>(defHttp.post({ url: prefix, data }));
}
