import { http } from './index';

// ==================== 教师课程 API ====================

export interface TeacherCourseQueryParams {
  page?: number;
  pageSize?: number;
  grade?: string;
  keyword?: string;
}

export interface TeacherCourse {
  id: number;
  name: string;
  pictureBookName?: string;
  coverImagePath?: string;
  gradeTags: string[];
  domainTags: string[];
  duration: number;
  avgRating: number;
  usageCount: number;
  publishedAt: string;
}

// 教师班级信息（更新：新增角色字段）
export interface TeacherClass {
  id: number;
  name: string;
  grade: string;
  studentCount: number;
  lessonCount: number;
  myRole: 'MAIN' | 'ASSIST' | 'CARE';  // 我在该班级的角色
  isPrimary: boolean;                    // 是否班主任
}

// 班级教师信息
export interface TeacherClassTeacher {
  teacherId: number;
  teacherName: string;
  teacherPhone?: string;
  role: 'MAIN' | 'ASSIST' | 'CARE';
  isPrimary: boolean;
}

// 获取教师可用的课程列表
export function getTeacherCourses(params: TeacherCourseQueryParams): Promise<{
  items: TeacherCourse[];
  total: number;
  page: number;
  pageSize: number;
}> {
  return http.get('/teacher/courses', { params });
}

// 获取课程详情
export function getTeacherCourse(id: number): Promise<any> {
  return http.get(`/teacher/courses/${id}`);
}

// 获取教师的班级列表
export function getTeacherClasses(): Promise<TeacherClass[]> {
  return http.get('/teacher/courses/classes');
}

// 获取教师所有学生列表（跨班级）
export function getTeacherStudents(params?: { page?: number; pageSize?: number; keyword?: string }): Promise<{
  items: Array<{
    id: number;
    name: string;
    gender?: string;
    birthDate?: string;
    classId: number;
    class?: {
      id: number;
      name: string;
      grade: string;
    };
    parentName?: string;
    parentPhone?: string;
    createdAt: string;
  }>;
  total: number;
  page: number;
  pageSize: number;
}> {
  return http.get('/teacher/students', { params });
}

// 获取班级学生列表
export function getTeacherClassStudents(classId: number, params?: { page?: number; pageSize?: number; keyword?: string }): Promise<{
  items: Array<{
    id: number;
    name: string;
    gender?: string;
    birthDate?: string;
    parentName?: string;
    parentPhone?: string;
    lessonCount?: number;
    readingCount?: number;
    createdAt: string;
  }>;
  total: number;
  page: number;
  pageSize: number;
  class?: {
    id: number;
    name: string;
    grade: string;
    studentCount: number;
    lessonCount: number;
  };
}> {
  return http.get(`/teacher/classes/${classId}/students`, { params });
}

// 获取班级教师列表
export function getClassTeachers(classId: number): Promise<TeacherClassTeacher[]> {
  return http.get(`/teacher/classes/${classId}/teachers`);
}

// ==================== 授课记录 API ====================

export interface CreateLessonDto {
  courseId: number;
  classId: number;
  plannedDatetime?: string;
}

export interface FinishLessonDto {
  overallRating?: string;
  participationRating?: string;
  completionNote?: string;
  actualDuration?: number;
}

export interface StudentRecordDto {
  focus?: number;
  participation?: number;
  interest?: number;
  understanding?: number;
  notes?: string;
}

// 获取授课记录列表
export function getLessons(params?: {
  page?: number;
  pageSize?: number;
  status?: string;
  courseId?: number;
}): Promise<{
  items: any[];
  total: number;
  page: number;
  pageSize: number;
}> {
  return http.get('/teacher/lessons', { params });
}

// 获取单个授课记录详情
export function getLesson(id: number): Promise<any> {
  return http.get(`/teacher/lessons/${id}`);
}

// 创建授课记录（备课）
export function createLesson(data: CreateLessonDto): Promise<any> {
  return http.post('/teacher/lessons', data);
}

// 开始上课
export function startLesson(id: number): Promise<any> {
  return http.post(`/teacher/lessons/${id}/start`);
}

// 结束上课
export function finishLesson(id: number, data: FinishLessonDto): Promise<any> {
  return http.post(`/teacher/lessons/${id}/finish`, data);
}

// 取消课程
export function cancelLesson(id: number): Promise<any> {
  return http.post(`/teacher/lessons/${id}/cancel`);
}

// 保存学生评价记录
export function saveStudentRecord(
  lessonId: number,
  studentId: number,
  data: StudentRecordDto
): Promise<any> {
  return http.post(`/teacher/lessons/${lessonId}/students/${studentId}/record`, data);
}

// 获取课程所有学生记录
export interface StudentWithRecord {
  id: number;
  name: string;
  gender?: string;
  record: {
    id: number;
    focus?: number;
    participation?: number;
    interest?: number;
    understanding?: number;
    notes?: string;
  } | null;
}

export interface StudentRecordsResponse {
  lesson: {
    id: number;
    status: string;
    className: string;
  };
  students: StudentWithRecord[];
}

export function getStudentRecords(lessonId: number): Promise<StudentRecordsResponse> {
  return http.get(`/teacher/lessons/${lessonId}/student-records`);
}

// 批量保存学生评价记录
export function batchSaveStudentRecords(
  lessonId: number,
  records: Array<{ studentId: number } & StudentRecordDto>
): Promise<{ count: number; records: any[] }> {
  return http.post(`/teacher/lessons/${lessonId}/student-records/batch`, { records });
}

// ==================== 教师首页 API ====================

export interface DashboardData {
  stats: {
    classCount: number;
    studentCount: number;
    lessonCount: number;
    courseCount: number;
  };
  todayLessons: Array<{
    id: number;
    courseId: number;
    courseName: string;
    pictureBookName?: string;
    classId: number;
    className: string;
    plannedDatetime: string;
    status: string;
    duration: number;
  }>;
  recommendedCourses: Array<{
    id: number;
    name: string;
    pictureBookName?: string;
    coverImagePath?: string;
    duration: number;
    usageCount: number;
    avgRating: number;
    gradeTags: string[];
  }>;
  weeklyStats: {
    lessonCount: number;
    studentParticipation: number;
    avgRating: number;
    totalDuration: number;
  };
  recentActivities: Array<{
    id: number;
    type: string;
    description: string;
    time: string;
  }>;
}

export const getTeacherDashboard = () =>
  http.get<DashboardData>('/teacher/dashboard');

export const getTodayLessons = () =>
  http.get<DashboardData['todayLessons']>('/teacher/dashboard/today');

export const getRecommendedCourses = () =>
  http.get<DashboardData['recommendedCourses']>('/teacher/dashboard/recommend');

export const getWeeklyStats = () =>
  http.get<DashboardData['weeklyStats']>('/teacher/dashboard/weekly');

// ==================== 教师统计趋势 ====================

export interface TeacherLessonTrendItem {
  month: string;
  lessonCount: number;
  avgRating: number;
}

export interface TeacherCourseUsageItem {
  name: string;
  value: number;
}

export const getTeacherLessonTrend = (months?: number) =>
  http.get<TeacherLessonTrendItem[]>('/teacher/dashboard/lesson-trend', { params: { months } });

export const getTeacherCourseUsage = () =>
  http.get<TeacherCourseUsageItem[]>('/teacher/dashboard/course-usage');

// ==================== 课程反馈 API ====================

export interface FeedbackDto {
  designQuality?: number;
  participation?: number;
  goalAchievement?: number;
  stepFeedbacks?: any;
  pros?: string;
  suggestions?: string;
  activitiesDone?: any;
}

export interface LessonFeedback {
  id: number;
  lessonId: number;
  teacherId: number;
  designQuality?: number;
  participation?: number;
  goalAchievement?: number;
  stepFeedbacks?: any;
  pros?: string;
  suggestions?: string;
  activitiesDone?: any;
  createdAt: string;
  updatedAt: string;
  teacher?: {
    id: number;
    name: string;
  };
  lesson?: {
    id: number;
    startDatetime?: string;
    course: {
      id: number;
      name: string;
      pictureBookName?: string;
    };
    class: {
      id: number;
      name: string;
    };
  };
}

// 提交课程反馈
export function submitFeedback(lessonId: number, data: FeedbackDto): Promise<LessonFeedback> {
  return http.post(`/teacher/lessons/${lessonId}/feedback`, data);
}

// 获取课程反馈
export function getFeedback(lessonId: number): Promise<LessonFeedback | null> {
  return http.get(`/teacher/lessons/${lessonId}/feedback`);
}

// ==================== 学校端反馈 API ====================

export interface FeedbackQueryParams {
  page?: number;
  pageSize?: number;
  teacherId?: number;
  courseId?: number;
}

export interface FeedbackStats {
  totalFeedbacks: number;
  avgDesignQuality: number;
  avgParticipation: number;
  avgGoalAchievement: number;
  courseStats: Record<number, { count: number; avgRating: number }>;
}

// 获取学校端反馈列表
export function getSchoolFeedbacks(params: FeedbackQueryParams): Promise<{
  items: LessonFeedback[];
  total: number;
  page: number;
  pageSize: number;
}> {
  return http.get('/school/feedbacks', { params });
}

// 获取反馈统计
export function getFeedbackStats(): Promise<FeedbackStats> {
  return http.get('/school/feedbacks/stats');
}

// 获取教师自己的反馈列表
export function getTeacherFeedbacks(params: FeedbackQueryParams): Promise<{
  items: LessonFeedback[];
  total: number;
  page: number;
  pageSize: number;
}> {
  return http.get('/teacher/feedbacks', { params });
}

// 获取教师自己的反馈统计
export function getTeacherFeedbackStats(): Promise<FeedbackStats> {
  return http.get('/teacher/feedbacks/stats');
}

// ==================== 排课管理 API ====================

export interface TeacherSchedule {
  id: number;
  classId: number;
  className: string;
  courseId: number;
  courseName: string;
  teacherId?: number;
  scheduledDate?: string;
  scheduledTime?: string;
  weekDay?: number;
  repeatType: 'NONE' | 'DAILY' | 'WEEKLY';
  source: 'SCHOOL' | 'TEACHER';
  status: 'ACTIVE' | 'CANCELLED';
  note?: string;
  hasLesson: boolean;
  lessonId?: number;
  lessonStatus?: string;
  createdAt: string;
}

export interface CreateTeacherScheduleDto {
  classId: number;
  courseId: number;
  scheduledDate?: string;
  scheduledTime?: string;
  weekDay?: number;
  repeatType?: 'NONE' | 'DAILY' | 'WEEKLY';
  repeatEndDate?: string;
  note?: string;
}

export interface TeacherTimetableItem {
  date: string;
  weekDay: number;
  schedules: TeacherSchedule[];
}

export const getTeacherSchedules = (params?: {
  startDate?: string;
  endDate?: string;
  status?: string;
  page?: number;
  pageSize?: number;
}) => http.get<{ items: TeacherSchedule[]; total: number; page: number; pageSize: number }>('/teacher/schedules', { params });

export const getTeacherTimetable = (params: { startDate: string; endDate: string }) =>
  http.get<TeacherTimetableItem[]>('/teacher/schedules/timetable', { params });

export const getTodayTeacherSchedules = () =>
  http.get<TeacherSchedule[]>('/teacher/schedules/today');

export const createTeacherSchedule = (data: CreateTeacherScheduleDto) =>
  http.post<TeacherSchedule>('/teacher/schedules', data);

export const updateTeacherSchedule = (id: number, data: Partial<CreateTeacherScheduleDto> & { status?: string }) =>
  http.put<TeacherSchedule>(`/teacher/schedules/${id}`, data);

export const cancelTeacherSchedule = (id: number) =>
  http.delete<{ message: string }>(`/teacher/schedules/${id}`);

// ==================== 阅读任务 API ====================

export interface TeacherTask {
  id: number;
  tenantId: number;
  title: string;
  description?: string;
  taskType: 'READING' | 'ACTIVITY' | 'HOMEWORK';
  targetType: 'CLASS' | 'STUDENT';
  status: 'DRAFT' | 'PUBLISHED' | 'ARCHIVED';
  relatedCourseId?: number;
  startDate: string;
  endDate: string;
  createdBy: number;
  createdAt: string;
  updatedAt: string;
  course?: {
    id: number;
    name: string;
  };
  targetCount?: number;
  completionCount?: number;
}

export interface TaskCompletion {
  id: number;
  taskId: number;
  studentId: number;
  status: 'PENDING' | 'IN_PROGRESS' | 'COMPLETED';
  completedAt?: string;
  feedback?: string;
  parentFeedback?: string;
  createdAt: string;
  student: {
    id: number;
    name: string;
    gender?: string;
    class?: {
      id: number;
      name: string;
    };
  };
}

export interface CreateTeacherTaskDto {
  title: string;
  description?: string;
  taskType: 'READING' | 'ACTIVITY' | 'HOMEWORK';
  targetType: 'CLASS' | 'STUDENT';
  targetIds: number[];
  relatedCourseId?: number;
  startDate: string;
  endDate: string;
}

export interface UpdateTaskCompletionDto {
  status: 'PENDING' | 'IN_PROGRESS' | 'COMPLETED';
  feedback?: string;
}

export const getTeacherTasks = (params?: {
  page?: number;
  pageSize?: number;
  status?: string;
  taskType?: string;
  keyword?: string;
}) => http.get<{ items: TeacherTask[]; total: number; page: number; pageSize: number }>('/teacher/tasks', { params });

export const getTeacherTask = (id: number) =>
  http.get<TeacherTask>(`/teacher/tasks/${id}`);

export const getTeacherTaskCompletions = (taskId: number, params?: {
  page?: number;
  pageSize?: number;
  status?: string;
}) => http.get<{ items: TaskCompletion[]; total: number; page: number; pageSize: number }>(`/teacher/tasks/${taskId}/completions`, { params });

export const createTeacherTask = (data: CreateTeacherTaskDto) =>
  http.post<TeacherTask>('/teacher/tasks', data);

export const updateTeacherTask = (id: number, data: Partial<CreateTeacherTaskDto> & { status?: string }) =>
  http.put<TeacherTask>(`/teacher/tasks/${id}`, data);

export const deleteTeacherTask = (id: number) =>
  http.delete<{ message: string }>(`/teacher/tasks/${id}`);

export const updateTaskCompletion = (taskId: number, studentId: number, data: UpdateTaskCompletionDto) =>
  http.put<TaskCompletion>(`/teacher/tasks/${taskId}/completions/${studentId}`, data);

export const sendTaskReminder = (taskId: number) =>
  http.post<{ message: string }>(`/teacher/tasks/${taskId}/remind`);

// ==================== 任务模板 API ====================

export interface TaskTemplate {
  id: number;
  tenantId: number;
  name: string;
  description?: string;
  taskType: 'READING' | 'ACTIVITY' | 'HOMEWORK';
  relatedCourseId?: number;
  defaultDuration: number;
  isDefault: boolean;
  status: string;
  createdBy: number;
  createdAt: string;
  updatedAt: string;
  course?: {
    id: number;
    name: string;
    pictureBookName?: string;
  };
}

export interface CreateTaskTemplateDto {
  name: string;
  description?: string;
  taskType: 'READING' | 'ACTIVITY' | 'HOMEWORK';
  relatedCourseId?: number;
  defaultDuration?: number;
  isDefault?: boolean;
}

export interface CreateTaskFromTemplateDto {
  templateId: number;
  targetIds: number[];
  targetType: 'CLASS' | 'STUDENT';
  startDate?: string;
}

export const getTaskTemplates = (params?: {
  page?: number;
  pageSize?: number;
  taskType?: string;
  keyword?: string;
}) => http.get<{ items: TaskTemplate[]; total: number; page: number; pageSize: number }>('/teacher/task-templates', { params });

export const getTaskTemplate = (id: number) =>
  http.get<TaskTemplate>(`/teacher/task-templates/${id}`);

export const getDefaultTaskTemplate = (taskType: string) =>
  http.get<TaskTemplate | null>(`/teacher/task-templates/default/${taskType}`);

export const createTaskFromTemplate = (data: CreateTaskFromTemplateDto) =>
  http.post<TeacherTask>('/teacher/tasks/from-template', data);

// ==================== 任务统计 API ====================

export interface TaskStats {
  totalTasks: number;
  publishedTasks: number;
  completedTasks: number;
  inProgressTasks: number;
  pendingCount: number;
  totalCompletions: number;
  completionRate: number;
}

export interface TaskStatsByType {
  [key: string]: {
    total: number;
    completed: number;
    rate: number;
  };
}

export interface TaskStatsByClass {
  classId: number;
  className: string;
  grade: string;
  total: number;
  completed: number;
  rate: number;
}

export interface MonthlyTaskStats {
  month: string;
  tasks: number;
  completions: number;
  completed: number;
  rate: number;
}

export const getTaskStats = () =>
  http.get<TaskStats>('/teacher/tasks/stats');

export const getTaskStatsByType = () =>
  http.get<TaskStatsByType>('/teacher/tasks/stats/by-type');

export const getTaskStatsByClass = () =>
  http.get<TaskStatsByClass[]>('/teacher/tasks/stats/by-class');

export const getMonthlyTaskStats = (months?: number) =>
  http.get<MonthlyTaskStats[]>('/teacher/tasks/stats/monthly', { params: { months } });
