import type { HtyUser as CommonsHtyUser } from '@authcore/commons';

export type {
  HtyErr,
  HtyResponse,
  KVPair,
  CommonMeta,
  MultiVals,
  SingleVal,
  PagedQueryParam,
  HtyStyle,
  HtyVerified,
  WeixinUserInfo,
  HtyUserSetting,
  HtyUserInfoRole,
  HtyApplyUser,
  TeacherStudent,
  HtyRoles,
  HtyTag,
  HtyTagRef,
  HtyTagRefsByRefId,
  TongzhiQueryParam,
  NotifyParam,
  CommonTask,
  TaskMeta,
  GroupUser,
  UserGroup,
} from '@authcore/commons';

export {
  HtyErrCode,
  UnauthorizedError,
  TeacherStudentStates,
  HtyStates,
  HtyBaseRoles,
  HtySuperRoles,
  TongzhiStatuses,
  TaskFrom,
  TaskStatuses,
  TaskTypes,
  GroupTypes,
} from '@authcore/commons';

export { NotifyTypes } from './domain/notifyTypes';
export type { TongzhiContent } from './domain/tongzhiContent';
export type { Tongzhi, TongzhiPushInfo } from './domain/tongzhi';

export type { TeacherInfo, StudentInfo } from '@authcore/commons';

export interface HtyUser extends CommonsHtyUser {
  supervisor?: TeacherSummary;
}
export type { HtyUserApp, HtyRole, HtyLabel } from '@authcore/commons';

export interface TeacherSummary {
  teacher_id: string;
  teacher_name: string;
}

export interface Organization {
  id: string;
  app_id: string;
  org_name: string;
  org_desc?: string;
  homepage_md?: string;
  org_status: string;
  is_delete?: boolean;
}

export interface StudentSummary {
  student_id: string;
  student_name: string;
}

export type User = HtyUser & import('@authcore/commons').HtyUserApp;

export enum TaskStatus {
  NOOP = 'NOOP',
  DONE = 'DONE',
  PENDING = 'PENDING',
  PROCESSING = 'PROCESSING',
  FAILED = 'FAILED',
}

export enum AiTaskStatuses {
  REVOKED = 'REVOKED',
  RETRY = 'RETRY',
  RECEIVED = 'RECEIVED',
  PENDING = 'PENDING',
  FAILED = 'FAILED',
  STARTED = 'STARTED',
  SUCCESS = 'SUCCESS',
}

export type AudioConvertTask = import('@authcore/commons').CommonTask & {
  media_id: string;
  task_type: import('@authcore/commons').TaskTypes;
};

export interface CourseQueryParam {
  page: number;
  page_size: number;
  keyword?: string;
  created_by?: string;
  teacher_id?: string;
}

export interface JihuaQueryParam {
  start_date?: string;
  end_date?: string;
  page: number;
  page_size: number;
  teacher_id?: string;
  student_id?: string;
  scope?: DakaScope;
}

export enum ScoreRefTypes {
  Lianxi = 'Lianxi',
  Comment = 'Comment',
}

export enum ScoreTargets {
  Video = 'Video',
  Audio = 'Audio',
}

export enum ScoreFroms {
  Matlab = 'Matlab',
  MachineLearning = 'MachineLearning',
}

export enum ScoreTypes {
  Recall = 'recall',
  Accuracy = 'accuracy',
}

export interface ScoreResult {
  score_val: number;
  score_type: ScoreTypes;
  score_from: ScoreFroms;
}

export interface Score {
  id?: string;
  ref_id: string;
  ref_type: ScoreRefTypes;
  score_val?: number;
  score_from: ScoreFroms;
  created_at?: Date | string;
  created_by?: string;
  meta?: import('@authcore/commons').CommonMeta;
  tasks?: import('@authcore/commons').MultiVals<import('@authcore/commons').CommonTask>;
  multi_scores?: import('@authcore/commons').MultiVals<ScoreResult>;
}

export interface RefResource {
  id?: string;
  parent_id?: string;
  hty_resource_id?: string;
  is_shifan?: boolean;
  ref_id?: string;
  ref_type?: string;
  ref_name?: string;
  ref_desc?: string;
  resource_url?: string;
  resource_type: string;
  tasks?: import('@authcore/commons').MultiVals<import('@authcore/commons').CommonTask>;
  wx_cache?: Partial<WxUploadingPhoto>;
}

export interface Lianxi {
  id: string;
  jihua_course_section_id?: string;
  daka_course_section_id?: string;
  qupu?: RefResource;
  video?: RefResource;
  audio?: RefResource;
  text_question: string;
  created_at: string | Date;
  task: {
    tasks: [UploadPictureTask | null, AudioConvertTask | null, AiTask | null];
  };
  piyue?: Piyue;
  piyues?: Piyue[];
  is_delete?: boolean;
  scores?: Score[];
  created_by?: string;
  creator_name?: string;
  ref_resources: RefResource[];
}

export interface Rating {
  stars: number;
  comment?: CommentContent;
}

export interface CommentContent {
  text?: string;
  meta?: string;
  audio?: RefResource;
  task?: AudioConvertTask;
  video?: RefResource;
  photos?: RefResource[];
}

export enum CommentRefTypes {
  Piyue = 'PIYUE',
  Jihua = 'JIHUA',
  Daka = 'DAKA',
  ResourceNote = 'RESOURCE_NOTE',
  Clazz = 'CLAZZ',
}

export interface Comment {
  id?: string;
  ref_id?: string;
  ref_type: CommentRefTypes;
  parent_id?: string;
  created_at?: Date | string;
  created_by?: string;
  creator_name?: string;
  content: CommentContent;
  has_piyue?: boolean;
  ref_resources?: RefResource[];
  resource_note_group?: ResourceNoteGroup;
  scores?: Score[];
  creator_role_key?: import('@authcore/commons').HtyRoles;
}

export interface PiyueInfo {
  id: string;
  piyue_id?: string;
  serial: string;
  lianxi_video_pos?: ShipinPosition;
  qupu_pos?: QupuPosition;
  comment?: Comment;
  comments: Comment[];
}

export interface Piyue {
  id?: string;
  lianxi_id: string;
  piyue_infos?: PiyueInfo[];
  teacher_id?: string;
  teacher_name?: string;
  rating: Rating;
  answer?: CommentContent;
  comments: Comment[];
  resource_note_group?: ResourceNoteGroup;
}

export interface QupuPosition {
  lng: number;
  lat: number;
}

export interface ShipinPosition {
  time: number;
}

export interface JihuaCourseSection {
  id: string;
  jihua_id?: string;
  course_section_id?: string;
  meta?: unknown;
}

export type UploadPictureTask = import('@authcore/commons').CommonTask & {
  task_type: import('@authcore/commons').TaskTypes;
};

export type AiTask = import('@authcore/commons').CommonTask & {
  task_type: import('@authcore/commons').TaskTypes.AI_SCORE;
};

export type AudioAiScoreTask = import('@authcore/commons').CommonTask & {
  task_type: 'AUDIO_FILE_AI_SCORE';
  payload: {
    aiScorePayload: AiScoreData;
    audioPayload: { media_id: string };
  };
};

export type VideoCompressTask = import('@authcore/commons').CommonTask & {
  task_type: import('@authcore/commons').TaskTypes;
};

export enum RefResourceTypes {
  Video = 'Video',
  Picture = 'Picture',
  Audio = 'Audio',
}

export enum RefTypes {
  CourseSection = 'CourseSection',
}

export interface CourseCategory {
  id: string;
  category_key: string;
  category_name: string;
  category_desc?: string;
  created_by: string;
  created_at: Date | string;
  is_delete: boolean;
  is_default?: boolean;
}

export interface Course {
  id?: string;
  course_name: string;
  course_type: string;
  created_by?: string;
  created_at?: Date | string;
  is_delete?: boolean;
  sections?: CourseSection[];
}

export interface CourseGroup {
  id?: string;
  group_name: string;
  teachers?: import('@authcore/commons').MultiVals<TeacherSummary>;
  course_section_ids?: import('@authcore/commons').MultiVals<string>;
  course_sections?: CourseSection[];
  created_by?: string;
  created_at?: Date | string;
  creator_name?: string;
  updated_by?: string;
  updated_at?: Date | string;
  updator_name?: string;
  org_id?: string;
  org_visible?: boolean;
}

export interface ResourceNoteGroup {
  id?: string;
  ref_id?: string;
  ref_type?: string;
  ref_resource_ids?: import('@authcore/commons').MultiVals<string>;
  resource_note_ids?: import('@authcore/commons').MultiVals<string>;
  meta?: import('@authcore/commons').CommonMeta;
  is_delete?: boolean;
  created_at?: Date | string;
  created_by?: string;
  resource_notes?: ResourceNote[];
}

export interface CourseSection {
  id?: string;
  course_id?: string;
  course_name?: string;
  course_category_key?: string;
  course_category_name?: string;
  course_text?: string;
  section_name: string;
  shifan_id?: string;
  shifan_url?: string;
  created_at?: Date | string;
  created_by?: string;
  creator_name?: string;
  updated_at?: Date | string;
  updated_by?: string;
  updator_name?: string;
  task?: UploadPictureTask;
  jihua_course_section_id?: string;
  lianxis?: Lianxi[];
  lianxi_count?: number;
  not_piyue_count?: number;
  is_delete?: boolean;
  resources?: RefResource[];
  qupu_text?: string;
  resource_note_group?: ResourceNoteGroup;
  is_draft?: boolean;
  cloned_from?: string;
  ref_resources?: RefResource[];
}

export interface Jihua {
  id: string;
  start_date: Date | string;
  end_date: Date | string;
  yanqi_start_date?: Date | string;
  yanqi_end_date?: Date | string;
  student_id: string;
  student_name: string;
  teacher_id: string;
  teacher_name: string;
  beizhu: string;
  course_sections: CourseSection[];
  course_sections2?: CourseSection[];
  lianxi_count?: number;
  is_yanqi?: boolean;
  not_piyue_count?: number;
  parent_id?: string;
  is_delete: boolean;
  relations?: JihuaCourseSection[];
  versioned_data?: VersionedData[];
}

export interface UploadedFile {
  hty_resource_id: string;
  url: string;
}

export interface FormUploadingFile {
  file: File;
  status: string;
  message: string;
  content: string;
}

export interface WxUploadingPhoto {
  localId: string;
  localData: string;
  serverId: string;
}

export type UploadPhoto = UploadedFile & FormUploadingFile & WxUploadingPhoto;

export interface HtyTeacher {
  academic?: string;
  graduated_from?: string;
  experience?: string;
  subjects?: string[];
}

export interface HtyStudent {
  experience?: string;
}

export const RatingTexts = [
  '还没有评分',
  '加油哦！',
  '还需努力哦！',
  '基本达标，再接再厉！',
  '非常棒！',
  '完美！',
];

export enum DakaScope {
  ALL = 'ALL',
  CREATED = 'CREATED',
  ASSIGNED = 'ASSIGNED',
}

export enum UserStates {
  Anonymous,
  Authorized,
  Registered,
}

export enum WxScenes {
  Other = 1000,
  Discover = 1001,
  TemplateMessage = 1043,
  RecentUsed = 1089,
  Favorite = 1271,
}

export interface Daka {
  id: string;
  name: string;
  desc?: string;
  start_date: Date | string;
  duration_days: number;
  end_date?: Date | string;
  teacher_id?: string;
  teacher_name?: string;
  group_id?: string;
  group_name?: string;
  created_at?: Date | string;
  created_by?: string;
  course_sections: CourseSection[];
  course_sections2?: import('@authcore/commons').MultiVals<CourseSection>;
  relations: DakaCourseSection[];
  lianxi_count?: number;
  is_delete?: boolean;
  groups?: import('@authcore/commons').UserGroup[];
  teachers?: import('@authcore/commons').MultiVals<TeacherSummary>;
  students?: import('@authcore/commons').SingleVal<import('@authcore/commons').UserGroup>;
  clazz?: Clazz;
  is_yanqi?: boolean;
}

export enum UserRelTypes {
  StudentToTeacher = 'StudentToTeacher',
  TeacherToTeacher = 'TeacherToTeacher',
}

export interface UserRel {
  id: string;
  from_user_id: string;
  from_user_realname: string;
  to_user_id: string;
  to_user_realname: string;
  rel_type: UserRelTypes;
}

export interface DakaCourseSection {
  daka_id: string;
  course_section_id: string;
  is_delete: boolean;
  id: string;
}

export enum PickTargets {
  JIHUA = 'JIHUA',
  DAKA = 'DAKA',
  USER_GROUP = 'USER_GROUP',
  COURSE_SECTION = 'COURSE_SECTION',
  COURSE_GROUP = 'COURSE_GROUP',
  CLAZZ = 'CLAZZ',
  COURSE_PACKAGE = 'COURSE_PACKAGE',
}

export interface LatitudePos {
  lng: number;
  lat: number;
}

export interface TimestampPos {
  time: number;
}

export enum ResourceNoteTypes {
  Alphabet = 'ALPHABET',
  Line = 'LINE',
  Rect = 'RECT',
}

export interface ResourcePos {
  ref_resource_id?: string;
  hty_resource_id?: string;
  lat_pos?: LatitudePos;
  time_pos?: TimestampPos;
  multi_lat_pos?: {
    points: import('@authcore/commons').MultiVals<LatitudePos>;
    pos_type?: ResourceNoteTypes;
  };
  directed_lat_pos?: {
    points: import('@authcore/commons').MultiVals<LatitudePos>;
    from_tos: import('@authcore/commons').MultiVals<[string, string]>;
    pos_type?: ResourceNoteTypes;
  };
}

export interface ResourceNote {
  id?: string;
  created_at?: Date | string;
  created_by?: string;
  serial_no: string;
  note_type?: ResourceNoteTypes;
  is_delete?: boolean;
  resource_pos?: import('@authcore/commons').MultiVals<ResourcePos>;
  comments?: Comment[];
  comment?: Comment;
  lat_pos?: LatitudePos;
  time_pos?: TimestampPos;
  marker?: object;
  meta?: import('@authcore/commons').CommonMeta;
}

export interface NotedResources {
  qupu?: RefResource;
  video?: RefResource;
  audio?: RefResource;
}

export interface PoolData {
  jihua_id?: string;
  daka_id?: string;
  lianxi_id?: string;
  piyue_id?: string;
  course_section?: CourseSection;
  business_type?: PickTargets;
  current_role?: string;
  hide_bottom_tab?: boolean;
  scoring_audio?: RefResource;
  clazz_state?: unknown;
  course_sections?: CourseSection[];
  teachers?: import('@authcore/commons').SingleVal<import('@authcore/commons').UserGroup>;
  students?: import('@authcore/commons').SingleVal<import('@authcore/commons').UserGroup>;
}

export enum TimeUnits {
  DAY = 'DAY',
  HOUR = 'HOUR',
  MINUTE = 'MINUTE',
  SECOND = 'SECOND',
}

export enum ClazzStatuses {
  Open = 'OPEN',
}

export enum ClazzRepeatStatuses {
  OnGoing = 'ON_GOING',
}

export interface ClazzRepeat {
  id: string;
  clazz_id: string;
  start_from?: string | Date;
  end_by?: string | Date;
  repeat_start: string | Date;
  repeat_end?: string | Date;
  repeat_cycle_days: number;
  repeat_status: ClazzRepeatStatuses;
  latest_clazz_created_at?: string | Date;
}

export interface ClazzUser {
  id: string;
  user_id: string;
  clazz_id: string;
  clazz_status: ClazzStatuses;
  user_type: import('@authcore/commons').HtyBaseRoles;
}

export interface Clazz {
  id: string;
  clazz_name: string;
  clazz_desc: string;
  clazz_status: ClazzStatuses;
  start_from: string | Date;
  end_by: string | Date;
  duration: number;
  clazz_type: string;
  root_id: string;
  parent_id: string;
  teachers: import('@authcore/commons').SingleVal<import('@authcore/commons').UserGroup>;
  students: import('@authcore/commons').SingleVal<import('@authcore/commons').UserGroup>;
  created_by: string;
  created_at: string | Date;
  dakas?: import('@authcore/commons').MultiVals<Daka>;
  jihuas?: import('@authcore/commons').MultiVals<Jihua>;
  is_delete?: boolean;
  clazz_repeat?: ClazzRepeat;
  is_repeat: boolean;
  repeat_start?: string | Date;
  repeat_end?: string | Date;
  instance?: Clazz;
  has_root?: boolean;
  is_notified?: boolean;
  /** 业务已结课：首次点名落库时间；未点名或未结课为 null */
  completed_at?: string | Date | null;
  course_sections?: import('@authcore/commons').MultiVals<CourseSection>;
}

/** 与后端 ReqClazzWithRepeat（扁平 join）字段一致：clz_* 主表，re_* 重复规则 */
export interface ClazzWithRepeat {
  clz_id: string;
  clz_clazz_name: string;
  clz_clazz_desc: string;
  clz_clazz_status: ClazzStatuses;
  clz_start_from: string | Date;
  clz_end_by: string | Date;
  clz_duration: number;
  clz_clazz_type: string;
  clz_root_id: string;
  clz_parent_id: string;
  clz_teachers: import('@authcore/commons').SingleVal<import('@authcore/commons').UserGroup>;
  clz_students: import('@authcore/commons').SingleVal<import('@authcore/commons').UserGroup>;
  clz_created_by: string;
  clz_created_at: string | Date;
  clz_dakas?: import('@authcore/commons').MultiVals<Daka>;
  clz_jihuas?: import('@authcore/commons').MultiVals<Jihua>;
  clz_is_delete?: boolean;
  clz_is_repeat: boolean;
  clz_completed_at?: string | Date | null;
  re_id: string;
  re_clazz_id: string;
  re_start_from?: string | Date;
  re_end_by?: string | Date;
  re_repeat_start: string | Date;
  re_repeat_end?: string | Date;
  re_repeat_cycle_days: number;
  re_repeat_status: ClazzRepeatStatuses;
}

/** 排课消课记录（对应 htykc.clazz_attendance） */
export interface ClazzAttendance {
  id: string;
  clazz_id: string;
  student_id: string;
  course_hour_package_id?: string | null;
  status: string;
  deducted_hours: number;
  sign_time: string;
  sign_method?: string | null;
  created_at: string;
  created_by?: string | null;
  is_delete?: boolean | null;
}

export interface ReqClazzAttendanceItem {
  student_id: string;
  status: string;
  deducted_hours: number;
  sign_method?: string | null;
  course_hour_package_id?: string | null;
}

export interface ReqBatchClazzAttendance {
  clazz_id: string;
  items: ReqClazzAttendanceItem[];
}

/** 日历重复行：扁平 clazz + 当周实例 */
export type ClazzRepeatRow = Clazz & { instance?: Clazz };

export interface ScoreShifan {
  shifan_url?: string;
  audio_type?: string;
}

export interface AiScoreData {
  reference_url?: string;
  compare_url?: string;
  audio_type?: string;
  ref_type: ScoreRefTypes;
  ref_id: string;
  score_from: ScoreFroms;
}

export enum VersionedDataTypes {
  DateRange = 'DATE_RANGE',
}

export interface DateRangeData {
  start_date: string | Date;
  end_date: string | Date;
}

export interface VersionedData {
  id?: string;
  ref_id: string;
  ref_type: PickTargets;
  data_type: VersionedDataTypes;
  from_data: DateRangeData | unknown;
  to_data: DateRangeData | unknown;
  created_at?: string | Date;
  created_by?: string;
}

export enum UserSettingKeys {
  ReceiveCourseNotification = 'receive_course_notification',
}

export enum SwitchOptions {
  ON = 'ON',
  OFF = 'OFF',
}

export interface CoursePackage {
  id?: string;
  package_name: string;
  description?: string;
  ws_course_id?: string;
  total_lessons?: number;
  original_price?: number;
  selling_price?: number;
  validity_days?: number;
  package_status: string;
  cover_image_url?: string;
  sort_order?: number;
  created_by?: string;
  created_at?: string | Date;
  updated_at?: string | Date;
  is_delete?: boolean;
  org_id?: string;
  published_snapshot?: any;
  published_at?: string | Date;
}
