/**
 * Mock 课程计划数据（course-plans 域，P7-insights 扩展）
 *
 * 维护者：ai13（teacher-portal）
 *
 * 覆盖：
 * - 5 个课程计划（draft/published/archived）
 * - 每个含 4-6 章节（含学时/目标/重难点/活动）
 * - 关联教材 + 作业入口
 */

import type {
  CoursePlanItem,
  CoursePlanDetail,
  CoursePlanChapter,
  CreateCoursePlanInput,
  UpdateCoursePlanInput,
} from "@/lib/graphql-p7-insights";

interface CoursePlanDef {
  id: string;
  title: string;
  subject: string;
  grade: string;
  academicYear: string;
  semester: string;
  totalHours: number;
  status: "DRAFT" | "PUBLISHED" | "ARCHIVED";
  textbookId: string | null;
  textbookTitle: string | null;
  homeworkCount: number;
  chapterCount: number;
  updatedAt: string;
}

const COURSE_PLAN_DEFS: CoursePlanDef[] = [
  {
    id: "cp-001",
    title: "高三数学上学期课程计划",
    subject: "数学",
    grade: "高中三年级",
    academicYear: "2026-2027",
    semester: "上学期",
    totalHours: 64,
    status: "PUBLISHED",
    textbookId: null,
    textbookTitle: null,
    homeworkCount: 12,
    chapterCount: 6,
    updatedAt: "2026-07-01T09:00:00Z",
  },
  {
    id: "cp-002",
    title: "高一语文上学期课程计划",
    subject: "语文",
    grade: "高中一年级",
    academicYear: "2026-2027",
    semester: "上学期",
    totalHours: 48,
    status: "PUBLISHED",
    textbookId: "tb-001",
    textbookTitle: "小学语文三年级上册",
    homeworkCount: 8,
    chapterCount: 4,
    updatedAt: "2026-07-02T14:30:00Z",
  },
  {
    id: "cp-003",
    title: "初中英语七年级下学期课程计划",
    subject: "英语",
    grade: "初中一年级",
    academicYear: "2026-2027",
    semester: "下学期",
    totalHours: 36,
    status: "DRAFT",
    textbookId: "tb-003",
    textbookTitle: "初中英语七年级上册",
    homeworkCount: 6,
    chapterCount: 5,
    updatedAt: "2026-07-03T10:15:00Z",
  },
  {
    id: "cp-004",
    title: "小学数学六年级复习课程计划",
    subject: "数学",
    grade: "小学六年级",
    academicYear: "2026-2027",
    semester: "下学期",
    totalHours: 24,
    status: "ARCHIVED",
    textbookId: "tb-002",
    textbookTitle: "小学数学六年级上册",
    homeworkCount: 4,
    chapterCount: 4,
    updatedAt: "2026-06-28T09:30:00Z",
  },
  {
    id: "cp-005",
    title: "高三数学下学期复习冲刺",
    subject: "数学",
    grade: "高中三年级",
    academicYear: "2026-2027",
    semester: "下学期",
    totalHours: 48,
    status: "DRAFT",
    textbookId: null,
    textbookTitle: null,
    homeworkCount: 10,
    chapterCount: 5,
    updatedAt: "2026-07-08T13:45:00Z",
  },
];

/** 课程计划列表项 */
export const mockCoursePlans: CoursePlanItem[] = COURSE_PLAN_DEFS.map((p) => ({
  id: p.id,
  title: p.title,
  subject: p.subject,
  grade: p.grade,
  academicYear: p.academicYear,
  semester: p.semester,
  totalHours: p.totalHours,
  status: p.status,
  textbookId: p.textbookId,
  updatedAt: p.updatedAt,
}));

/** 按状态筛选 */
export function filterCoursePlans(
  status?: "DRAFT" | "PUBLISHED" | "ARCHIVED" | "ALL" | null,
): CoursePlanItem[] {
  if (!status || status === "ALL") return mockCoursePlans;
  return mockCoursePlans.filter((p) => p.status === status);
}

/** 生成章节（按章节模板） */
function generateChapters(planId: string, count: number): CoursePlanChapter[] {
  const template = [
    { title: "第一章 导入与基础", obj: "建立学科认知框架", kp: "基础概念与运算规则" },
    { title: "第二章 核心知识", obj: "掌握核心知识与方法", kp: "核心定理与推导" },
    { title: "第三章 综合应用", obj: "能够解决综合问题", kp: "应用场景与解题策略" },
    { title: "第四章 拓展提升", obj: "拓展学科思维", kp: "高阶方法与变式" },
    { title: "第五章 复习巩固", obj: "系统化复习", kp: "知识网络与方法对比" },
    { title: "第六章 评估反馈", obj: "阶段性评估", kp: "易错点与补强" },
  ];
  return Array.from({ length: count }, (_, i) => {
    const t = template[i] ?? template[0]!;
    return {
      id: `${planId}-ch-${String(i + 1).padStart(2, "0")}`,
      title: t.title,
      hours: 8 + i * 2,
      objectives: t.obj,
      keyPoints: t.kp,
      activities: `讲授 + 小组讨论 + 课堂练习 + 课后作业`,
    };
  });
}

/** 查询课程计划详情 */
export function findCoursePlanDetail(id: string): CoursePlanDetail | null {
  const found = COURSE_PLAN_DEFS.find((p) => p.id === id);
  if (!found) return null;
  return {
    id: found.id,
    title: found.title,
    subject: found.subject,
    grade: found.grade,
    academicYear: found.academicYear,
    semester: found.semester,
    totalHours: found.totalHours,
    status: found.status,
    textbookId: found.textbookId,
    textbookTitle: found.textbookTitle,
    homeworkCount: found.homeworkCount,
    chapters: generateChapters(found.id, found.chapterCount),
    updatedAt: found.updatedAt,
  };
}

/** 创建课程计划 */
export function createMockCoursePlan(
  input: CreateCoursePlanInput,
): CoursePlanItem {
  const now = new Date().toISOString();
  return {
    id: `cp-${String(Date.now()).slice(-6)}`,
    title: input.title,
    subject: input.subject,
    grade: input.grade,
    academicYear: input.academicYear,
    semester: input.semester,
    totalHours: input.totalHours,
    status: "DRAFT",
    textbookId: input.textbookId ?? null,
    updatedAt: now,
  };
}

/** 更新课程计划 */
export function updateMockCoursePlan(
  input: UpdateCoursePlanInput,
): CoursePlanItem | null {
  const found = COURSE_PLAN_DEFS.find((p) => p.id === input.id);
  if (!found) return null;
  const now = new Date().toISOString();
  return {
    id: found.id,
    title: input.title ?? found.title,
    subject: found.subject,
    grade: found.grade,
    academicYear: found.academicYear,
    semester: found.semester,
    totalHours: found.totalHours,
    status: input.status ?? found.status,
    textbookId: found.textbookId,
    updatedAt: now,
  };
}
