import type {
  ChildDto,
  ClassInfoDto,
  ExamDto,
  GradeDto,
  HomeworkDto,
  LearningTrendDto,
  StudentWeaknessDto,
  UserInfoDto,
  ViewportDto,
  ClassPerformanceDto,
  NotificationDto,
  NotificationPreferencesDto,
} from "./dtos.js";

/**
 * 固定 mock 数据（DEV_MODE=true 时使用）。
 *
 * 对齐 contract.md §4.2 mock 策略：
 * - parent-001（王家长，roles=["parent"]）
 * - student-001（李同学，class-001，三年级）
 * - student-002（李妹妹，class-002，一年级）
 *
 * 关键约束：iam.GetChildrenByParent mock 返回的 student_id
 * 必须与 core-edu mock 数据一致（student-001 + student-002），
 * 否则 ChildGuard 越权校验会失败。
 */

// === iam mock ===

export const MOCK_PARENT: UserInfoDto = {
  id: "parent-001",
  email: "parent@example.com",
  name: "王家长",
  roles: ["parent"],
  permissions: [
    "parent:dashboard:view",
    "parent:children:view",
    "parent:grades:view",
    "parent:homework:view",
    "parent:exams:view",
    "parent:analytics:view",
    "parent:notifications:view",
    "parent:preferences:update",
  ],
};

export const MOCK_CHILDREN: ChildDto[] = [
  {
    id: "student-001",
    name: "李同学",
    grade: "三年级",
    classId: "class-001",
    className: "三年级1班",
    gradeId: "grade-003",
  },
  {
    id: "student-002",
    name: "李妹妹",
    grade: "一年级",
    classId: "class-002",
    className: "一年级2班",
    gradeId: "grade-001",
  },
];

export const MOCK_VIEWPORTS: ViewportDto[] = [
  {
    key: "dashboard",
    label: "首页",
    route: "/parent/dashboard",
    icon: "home",
    sortOrder: "1",
    requiredPermission: "parent:dashboard:view",
  },
  {
    key: "children",
    label: "我的孩子",
    route: "/parent/children",
    icon: "users",
    sortOrder: "2",
    requiredPermission: "parent:children:view",
  },
  {
    key: "grades",
    label: "成绩",
    route: "/parent/grades",
    icon: "chart",
    sortOrder: "3",
    requiredPermission: "parent:grades:view",
  },
  {
    key: "notifications",
    label: "通知",
    route: "/parent/notifications",
    icon: "bell",
    sortOrder: "4",
    requiredPermission: "parent:notifications:view",
  },
];

// === core-edu mock ===

export const MOCK_CLASSES: Record<string, ClassInfoDto> = {
  "class-001": { id: "class-001", name: "三年级1班", gradeId: "grade-003" },
  "class-002": { id: "class-002", name: "一年级2班", gradeId: "grade-001" },
};

function buildMockGrades(studentId: string): GradeDto[] {
  const subjects = ["数学", "语文", "英语", "科学", "美术"];
  return subjects.map((subject, i) => ({
    id: `grade-${studentId}-${i + 1}`,
    studentId,
    examId: `exam-${studentId}-${i + 1}`,
    homeworkId: "",
    score: String(75 + i * 5),
    feedback: `${subject}表现良好，继续努力`,
    gradedBy: "teacher-001",
    createdAt: new Date(Date.now() - i * 86400000).toISOString(),
    updatedAt: new Date(Date.now() - i * 86400000).toISOString(),
  }));
}

export const MOCK_GRADES: Record<string, GradeDto[]> = {
  "student-001": buildMockGrades("student-001"),
  "student-002": buildMockGrades("student-002"),
};

function buildMockHomework(classId: string): HomeworkDto[] {
  return [
    {
      id: `hw-${classId}-1`,
      classId,
      title: "数学练习册第1-10页",
      description: "完成第1-10页所有习题",
      dueDate: new Date(Date.now() + 86400000).toISOString(),
      status: "NOT_SUBMITTED",
      createdBy: "teacher-001",
      createdAt: new Date(Date.now() - 86400000).toISOString(),
      updatedAt: new Date(Date.now() - 86400000).toISOString(),
    },
    {
      id: `hw-${classId}-2`,
      classId,
      title: "语文课文朗读",
      description: "朗读第3课课文3遍",
      dueDate: new Date(Date.now() + 2 * 86400000).toISOString(),
      status: "SUBMITTED",
      createdBy: "teacher-001",
      createdAt: new Date(Date.now() - 2 * 86400000).toISOString(),
      updatedAt: new Date(Date.now() - 86400000).toISOString(),
    },
    {
      id: `hw-${classId}-3`,
      classId,
      title: "英语单词抄写",
      description: "抄写Unit 2单词各5遍",
      dueDate: new Date(Date.now() - 86400000).toISOString(),
      status: "GRADED",
      createdBy: "teacher-001",
      createdAt: new Date(Date.now() - 3 * 86400000).toISOString(),
      updatedAt: new Date(Date.now() - 86400000).toISOString(),
    },
  ];
}

export const MOCK_HOMEWORK: Record<string, HomeworkDto[]> = {
  "class-001": buildMockHomework("class-001"),
  "class-002": buildMockHomework("class-002"),
};

function buildMockExams(classId: string): ExamDto[] {
  return [
    {
      id: `exam-${classId}-1`,
      classId,
      title: "数学期中考试",
      description: "三年级数学期中测试",
      examDate: new Date(Date.now() + 7 * 86400000).toISOString(),
      duration: "90",
      totalScore: "100",
      status: "PUBLISHED",
      createdBy: "teacher-001",
      createdAt: new Date(Date.now() - 7 * 86400000).toISOString(),
      updatedAt: new Date(Date.now() - 3 * 86400000).toISOString(),
    },
    {
      id: `exam-${classId}-2`,
      classId,
      title: "语文单元测试",
      description: "第1-3单元综合测试",
      examDate: new Date(Date.now() + 14 * 86400000).toISOString(),
      duration: "60",
      totalScore: "100",
      status: "DRAFT",
      createdBy: "teacher-001",
      createdAt: new Date(Date.now() - 5 * 86400000).toISOString(),
      updatedAt: new Date(Date.now() - 5 * 86400000).toISOString(),
    },
    {
      id: `exam-${classId}-3`,
      classId,
      title: "英语听力测试",
      description: "Unit 1-2 听力专项",
      examDate: new Date(Date.now() - 3 * 86400000).toISOString(),
      duration: "45",
      totalScore: "50",
      status: "SCORED",
      createdBy: "teacher-001",
      createdAt: new Date(Date.now() - 10 * 86400000).toISOString(),
      updatedAt: new Date(Date.now() - 2 * 86400000).toISOString(),
    },
  ];
}

export const MOCK_EXAMS: Record<string, ExamDto[]> = {
  "class-001": buildMockExams("class-001"),
  "class-002": buildMockExams("class-002"),
};

// === data-ana mock ===

function buildMockWeakness(studentId: string): StudentWeaknessDto {
  return {
    studentId,
    weakPoints: [
      {
        knowledgePointId: `kp-${studentId}-1`,
        title: "分数加减法",
        mastery: 0.45,
      },
      {
        knowledgePointId: `kp-${studentId}-2`,
        title: "阅读理解",
        mastery: 0.62,
      },
      {
        knowledgePointId: `kp-${studentId}-3`,
        title: "英语时态",
        mastery: 0.38,
      },
    ],
  };
}

export const MOCK_WEAKNESS: Record<string, StudentWeaknessDto> = {
  "student-001": buildMockWeakness("student-001"),
  "student-002": buildMockWeakness("student-002"),
};

function buildMockTrend(studentId: string): LearningTrendDto {
  const points = [];
  for (let i = 0; i < 7; i++) {
    points.push({
      date: Date.now() - (6 - i) * 86400000,
      score: 70 + i * 3 + (studentId === "student-001" ? 5 : 0),
    });
  }
  return { studentId, points };
}

export const MOCK_TREND: Record<string, LearningTrendDto> = {
  "student-001": buildMockTrend("student-001"),
  "student-002": buildMockTrend("student-002"),
};

function buildMockClassPerformance(classId: string): ClassPerformanceDto {
  return {
    classId,
    averageScore: 82.5,
    passRate: 0.92,
    scores: [
      { studentId: "student-001", score: 85, grade: "A" },
      { studentId: "student-002", score: 78, grade: "B" },
      { studentId: "student-003", score: 92, grade: "A" },
      { studentId: "student-004", score: 65, grade: "C" },
    ],
  };
}

export const MOCK_CLASS_PERFORMANCE: Record<string, ClassPerformanceDto> = {
  "class-001": buildMockClassPerformance("class-001"),
  "class-002": buildMockClassPerformance("class-002"),
};

// === msg mock ===

function buildMockNotifications(parentId: string): NotificationDto[] {
  const now = Date.now();
  return [
    {
      id: `notif-${parentId}-1`,
      userId: parentId,
      type: "GRADE",
      title: "成绩发布通知",
      content: "李同学的数学期中考试成绩已发布，得分85分",
      channel: "APP",
      isRead: false,
      childId: "student-001",
      createdAt: now - 3600000,
    },
    {
      id: `notif-${parentId}-2`,
      userId: parentId,
      type: "HOMEWORK",
      title: "作业批改完成",
      content: "李妹妹的语文课文朗读作业已批改",
      channel: "APP",
      isRead: false,
      childId: "student-002",
      createdAt: now - 7200000,
    },
    {
      id: `notif-${parentId}-3`,
      userId: parentId,
      type: "EXAM",
      title: "考试发布通知",
      content: "三年级1班数学期中考试已发布，请查看详情",
      channel: "APP",
      isRead: true,
      childId: "student-001",
      createdAt: now - 86400000,
    },
    {
      id: `notif-${parentId}-4`,
      userId: parentId,
      type: "ANNOUNCEMENT",
      title: "学校公告",
      content: "本周五下午家长会，请准时参加",
      channel: "APP",
      isRead: false,
      childId: undefined,
      createdAt: now - 172800000,
    },
    {
      id: `notif-${parentId}-5`,
      userId: parentId,
      type: "SYSTEM",
      title: "欢迎使用家长端",
      content: "欢迎使用 Edu 家长端，您可以在此查看孩子的学习情况",
      channel: "APP",
      isRead: true,
      childId: undefined,
      createdAt: now - 604800000,
    },
  ];
}

export const MOCK_NOTIFICATIONS: Record<string, NotificationDto[]> = {
  "parent-001": buildMockNotifications("parent-001"),
};

export const MOCK_NOTIFICATION_PREFS: Record<
  string,
  NotificationPreferencesDto
> = {
  "parent-001": {
    parentId: "parent-001",
    channels: ["APP", "WECHAT"],
    eventTypes: {
      gradeReleased: true,
      homeworkGraded: true,
      examPublished: true,
      attendanceAlert: false,
      schoolAnnouncement: true,
    },
  },
};
