import { http } from './index';

// ==================== 类型定义 ====================

export type TaskType = 'READING' | 'ACTIVITY' | 'HOMEWORK';
export type TargetType = 'CLASS' | 'STUDENT';
export type TaskStatus = 'DRAFT' | 'PUBLISHED' | 'ARCHIVED';
export type CompletionStatus = 'PENDING' | 'IN_PROGRESS' | 'COMPLETED';

export interface Task {
  id: number;
  tenantId: number;
  title: string;
  description?: string;
  taskType: TaskType;
  targetType: TargetType;
  relatedCourseId?: number;
  createdBy: number;
  startDate: string;
  endDate: string;
  status: TaskStatus;
  createdAt: string;
  updatedAt: string;
  course?: {
    id: number;
    name: string;
  };
  targetCount?: number;
  completionCount?: number;
}

export interface TaskCompletion {
  id: number;
  taskId: number;
  studentId: number;
  status: CompletionStatus;
  completedAt?: string;
  feedback?: string;
  parentFeedback?: string;
  createdAt: string;
  student?: {
    id: number;
    name: string;
    gender?: string;
    class?: {
      id: number;
      name: string;
    };
  };
}

export interface CreateTaskDto {
  title: string;
  description?: string;
  taskType: TaskType;
  targetType: TargetType;
  relatedCourseId?: number;
  startDate: string;
  endDate: string;
  targetIds: number[];
}

export interface UpdateTaskDto {
  title?: string;
  description?: string;
  startDate?: string;
  endDate?: string;
  status?: TaskStatus;
  targetIds?: number[];
}

export interface UpdateCompletionDto {
  status: CompletionStatus;
  feedback?: string;
  parentFeedback?: string;
}

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

// ==================== 学校端 API ====================

export const getTasks = (params?: {
  page?: number;
  pageSize?: number;
  status?: TaskStatus;
  taskType?: TaskType;
  keyword?: string;
}) =>
  http.get<{ items: Task[]; total: number; page: number; pageSize: number }>(
    '/school/tasks',
    { params }
  );

export const getTask = (id: number) =>
  http.get<Task>(`/school/tasks/${id}`);

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

export const createTask = (data: CreateTaskDto) =>
  http.post<Task>('/school/tasks', data);

export const updateTask = (id: number, data: UpdateTaskDto) =>
  http.put<Task>(`/school/tasks/${id}`, data);

export const deleteTask = (id: number) =>
  http.delete(`/school/tasks/${id}`);

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

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

// ==================== 教师端 API ====================

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

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

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

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

export const updateTeacherTask = (id: number, data: UpdateTaskDto) =>
  http.put<Task>(`/teacher/tasks/${id}`, data);

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

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

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