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

// 考勤记录表
export const attendance = mysqlTable(
  "core_edu_attendance",
  {
    id: char("id", { length: 36 }).notNull().primaryKey(),
    scheduleId: char("schedule_id", { length: 36 }).notNull(),
    studentId: char("student_id", { length: 36 }).notNull(),
    status: varchar("status", { length: 20 }).notNull(), // present | absent | late | leave
    remark: text("remark"),
    recordedBy: char("recorded_by", { length: 36 }).notNull(),
    schoolId: char("school_id", { length: 36 }).notNull(),
    createdAt: timestamp("created_at").notNull().defaultNow(),
    updatedAt: timestamp("updated_at").notNull().defaultNow().onUpdateNow(),
  },
  (table) => ({
    uniqSchedStudent: uniqueIndex("uniq_sched_student").on(
      table.scheduleId,
      table.studentId,
    ),
    idxAttendanceStudentId: index("idx_attendance_student_id").on(
      table.studentId,
    ),
    idxAttendanceScheduleId: index("idx_attendance_schedule_id").on(
      table.scheduleId,
    ),
  }),
);

export type Attendance = typeof attendance.$inferSelect;
export type NewAttendance = typeof attendance.$inferInsert;
