/**
 * Mock 作业数据（core-edu 域）
 *
 * 维护者：ai13（teacher-portal）
 */

import type { HomeworkItem, HomeworkDetail, HomeworkSubmission, AssignHomeworkInput } from "@/lib/graphql";

export function generateMockHomework(classId: string): HomeworkItem[] {
  const now = Date.now();
  return [
    {
      id: `${classId}-hw-001`,
      classId,
      title: "函数练习题",
      description: "课本 P45 第 1-10 题",
      dueDate: new Date(now + 3 * 24 * 60 * 60 * 1000).toISOString(),
      status: "NOT_SUBMITTED",
    },
    {
      id: `${classId}-hw-002`,
      classId,
      title: "导数应用题",
      description: null,
      dueDate: new Date(now + 7 * 24 * 60 * 60 * 1000).toISOString(),
      status: "SUBMITTED",
    },
    {
      id: `${classId}-hw-003`,
      classId,
      title: "月考错题订正",
      description: "订正月考全部错题，并写解题思路",
      dueDate: new Date(now - 2 * 24 * 60 * 60 * 1000).toISOString(),
      status: "GRADED",
    },
  ];
}

/**
 * 从 homeworkId 反查 classId（mock 数据 ID 规约：${classId}-hw-XXX）
 * 兜底：未匹配时返回 "mock-class"。
 */
function extractClassIdFromHomeworkId(homeworkId: string): string {
  const match = homeworkId.match(/^(.+)-hw-\d+$/);
  return match?.[1] ?? "mock-class";
}

/** 生成 mock 提交列表（固定 5 个学生提交，混合状态） */
function generateMockSubmissions(homeworkId: string): HomeworkSubmission[] {
  const now = Date.now();
  const defs: Array<{
    status: HomeworkSubmission["status"];
    score: number | null;
    feedback: string | null;
  }> = [
    {
      status: "GRADED",
      score: Math.floor(Math.random() * 20) + 80,
      feedback: "思路清晰，过程完整",
    },
    {
      status: "GRADED",
      score: Math.floor(Math.random() * 20) + 80,
      feedback: "注意步骤书写规范",
    },
    { status: "SUBMITTED", score: null, feedback: null },
    { status: "SUBMITTED", score: null, feedback: null },
    { status: "NOT_SUBMITTED", score: null, feedback: null },
  ];
  return defs.map((d, i) => {
    const studentId = `${homeworkId.slice(0, -3)}${String(i + 1).padStart(3, "0")}`;
    return {
      id: `${homeworkId}-sub-${String(i + 1).padStart(3, "0")}`,
      homeworkId,
      studentId,
      studentName: `学生${String(i + 1).padStart(2, "0")}`,
      status: d.status,
      score: d.score,
      feedback: d.feedback,
      submittedAt:
        d.status !== "NOT_SUBMITTED"
          ? new Date(now - i * 60 * 60 * 1000).toISOString()
          : null,
    };
  });
}

/** 按作业 ID 查询详情（含提交列表，MSW mock） */
export function findMockHomeworkDetail(homeworkId: string): HomeworkDetail {
  const classId = extractClassIdFromHomeworkId(homeworkId);
  const all = generateMockHomework(classId);
  const matched = all.find((h) => h.id === homeworkId);
  const base = matched ?? {
    id: homeworkId,
    classId,
    title: "作业详情（mock 兜底）",
    description: "Mock 数据兜底：未匹配到列表中的作业",
    dueDate: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000).toISOString(),
    status: "SUBMITTED" as const,
  };
  return {
    ...base,
    submissions: generateMockSubmissions(homeworkId),
  };
}

/** 创建新作业：基于 input 生成 HomeworkItem（含随机 ID） */
export function createMockHomework(input: AssignHomeworkInput): HomeworkItem {
  const randomId = Math.random().toString(36).slice(2, 10);
  return {
    id: `${input.classId}-hw-${randomId}`,
    classId: input.classId,
    title: input.title,
    description: input.description ?? null,
    dueDate: input.dueDate,
    status: "NOT_SUBMITTED",
  };
}
