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

// 作业主表
export const homework = mysqlTable(
  "core_edu_homework",
  {
    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"),
    dueDate: datetime("due_date").notNull(),
    gracePeriod: int("grace_period").notNull().default(300), // 秒，仲裁默认 300
    status: varchar("status", { length: 20 }).notNull().default("assigned"),
    schoolId: char("school_id", { length: 36 }).notNull(),
    createdBy: char("created_by", { length: 36 }).notNull(),
    createdAt: timestamp("created_at").notNull().defaultNow(),
    updatedAt: timestamp("updated_at").notNull().defaultNow().onUpdateNow(),
  },
  (table) => ({
    idxHomeworkClassStatus: index("idx_homework_class_status").on(
      table.classId,
      table.status,
    ),
    idxHomeworkCreatedBy: index("idx_homework_created_by").on(table.createdBy),
  }),
);

// 作业提交表
export const homeworkSubmissions = mysqlTable(
  "core_edu_homework_submissions",
  {
    id: char("id", { length: 36 }).notNull().primaryKey(),
    homeworkId: char("homework_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 }),
    feedback: text("feedback"),
    createdAt: timestamp("created_at").notNull().defaultNow(),
    updatedAt: timestamp("updated_at").notNull().defaultNow().onUpdateNow(),
  },
  (table) => ({
    uniqHwStudent: uniqueIndex("uniq_hw_student").on(
      table.homeworkId,
      table.studentId,
    ),
    idxHwSubmissionsHomeworkId: index("idx_hw_submissions_homework_id").on(
      table.homeworkId,
    ),
    idxHwSubmissionsStudentId: index("idx_hw_submissions_student_id").on(
      table.studentId,
    ),
  }),
);

// 作业答题表
export const homeworkAnswers = mysqlTable(
  "core_edu_homework_answers",
  {
    id: char("id", { length: 36 }).notNull().primaryKey(),
    submissionId: char("submission_id", { length: 36 }).notNull(),
    questionId: char("question_id", { length: 36 }).notNull(),
    answer: text("answer"),
    score: decimal("score", { precision: 6, scale: 2 }),
    teacherComment: text("teacher_comment"),
    isCorrect: boolean("is_correct"),
    createdAt: timestamp("created_at").notNull().defaultNow(),
    updatedAt: timestamp("updated_at").notNull().defaultNow().onUpdateNow(),
  },
  (table) => ({
    idxHwAnswersSubmissionId: index("idx_hw_answers_submission_id").on(
      table.submissionId,
    ),
    idxHwAnswersQuestionId: index("idx_hw_answers_question_id").on(
      table.questionId,
    ),
  }),
);

export type Homework = typeof homework.$inferSelect;
export type NewHomework = typeof homework.$inferInsert;
export type HomeworkSubmission = typeof homeworkSubmissions.$inferSelect;
export type NewHomeworkSubmission = typeof homeworkSubmissions.$inferInsert;
export type HomeworkAnswer = typeof homeworkAnswers.$inferSelect;
export type NewHomeworkAnswer = typeof homeworkAnswers.$inferInsert;
