import {
  mysqlTable,
  varchar,
  text,
  timestamp,
  char,
  datetime,
  int,
  decimal,
  index,
  uniqueIndex,
} from "drizzle-orm/mysql-core";

// 考试主表
export const exams = mysqlTable(
  "core_edu_exams",
  {
    id: char("id", { length: 36 }).notNull().primaryKey(),
    classId: char("class_id", { length: 36 }).notNull(),
    subjectId: char("subject_id", { length: 36 }).notNull(),
    title: varchar("title", { length: 200 }).notNull(),
    description: text("description"),
    examDate: datetime("exam_date").notNull(),
    duration: int("duration").notNull(), // 秒
    totalScore: decimal("total_score", { precision: 6, scale: 2 }).notNull(),
    status: varchar("status", { length: 20 }).notNull().default("draft"),
    statusChangedAt: timestamp("status_changed_at").notNull().defaultNow(),
    statusChangedBy: char("status_changed_by", { length: 36 }),
    schoolId: char("school_id", { length: 36 }).notNull(),
    createdBy: char("created_by", { length: 36 }).notNull(),
    archivedAt: datetime("archived_at"),
    createdAt: timestamp("created_at").notNull().defaultNow(),
    updatedAt: timestamp("updated_at").notNull().defaultNow().onUpdateNow(),
  },
  (table) => ({
    idxExamsClassStatus: index("idx_exams_class_status").on(
      table.classId,
      table.status,
    ),
    idxExamsSchoolDate: index("idx_exams_school_date").on(
      table.schoolId,
      table.examDate,
    ),
    idxExamsCreatedBy: index("idx_exams_created_by").on(table.createdBy),
  }),
);

// 考试题目关联表
export const examQuestions = mysqlTable(
  "core_edu_exam_questions",
  {
    id: char("id", { length: 36 }).notNull().primaryKey(),
    examId: char("exam_id", { length: 36 }).notNull(),
    questionId: char("question_id", { length: 36 }).notNull(),
    order: int("order").notNull(),
    score: decimal("score", { precision: 6, scale: 2 }).notNull(),
    questionType: varchar("question_type", { length: 30 }).notNull(),
    createdAt: timestamp("created_at").notNull().defaultNow(),
  },
  (table) => ({
    idxExamQuestionsExamId: index("idx_exam_questions_exam_id").on(
      table.examId,
    ),
    idxExamQuestionsQuestionId: index("idx_exam_questions_question_id").on(
      table.questionId,
    ),
  }),
);

// 考试提交表
export const examSubmissions = mysqlTable(
  "core_edu_exam_submissions",
  {
    id: char("id", { length: 36 }).notNull().primaryKey(),
    examId: char("exam_id", { length: 36 }).notNull(),
    studentId: char("student_id", { length: 36 }).notNull(),
    status: varchar("status", { length: 20 })
      .notNull()
      .default("not_submitted"),
    submittedAt: datetime("submitted_at"),
    gradedAt: datetime("graded_at"),
    gradedBy: char("graded_by", { length: 36 }),
    totalScore: decimal("total_score", { precision: 6, scale: 2 }),
    createdAt: timestamp("created_at").notNull().defaultNow(),
    updatedAt: timestamp("updated_at").notNull().defaultNow().onUpdateNow(),
  },
  (table) => ({
    uniqExamStudent: uniqueIndex("uniq_exam_student").on(
      table.examId,
      table.studentId,
    ),
    idxExamSubmissionsExamId: index("idx_exam_submissions_exam_id").on(
      table.examId,
    ),
    idxExamSubmissionsStudentId: index("idx_exam_submissions_student_id").on(
      table.studentId,
    ),
  }),
);

export type Exam = typeof exams.$inferSelect;
export type NewExam = typeof exams.$inferInsert;
export type ExamQuestion = typeof examQuestions.$inferSelect;
export type NewExamQuestion = typeof examQuestions.$inferInsert;
export type ExamSubmission = typeof examSubmissions.$inferSelect;
export type NewExamSubmission = typeof examSubmissions.$inferInsert;
