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

// 教师关联表（IAM 事件消费幂等）
export const teacherAssociations = mysqlTable(
  "core_edu_teacher_associations",
  {
    id: char("id", { length: 36 }).notNull().primaryKey(),
    teacherId: char("teacher_id", { length: 36 }).notNull(),
    classId: char("class_id", { length: 36 }).notNull(),
    subjectId: char("subject_id", { length: 36 }),
    schoolId: char("school_id", { length: 36 }).notNull(),
    source: varchar("source", { length: 20 }).notNull().default("iam_event"),
    createdAt: timestamp("created_at").notNull().defaultNow(),
  },
  (table) => ({
    uniqTeacherClassSubject: uniqueIndex("uniq_teacher_class_subject").on(
      table.teacherId,
      table.classId,
      table.subjectId,
    ),
    idxTeacherAssocTeacherId: index("idx_teacher_assoc_teacher_id").on(
      table.teacherId,
    ),
  }),
);

export type TeacherAssociation = typeof teacherAssociations.$inferSelect;
export type NewTeacherAssociation = typeof teacherAssociations.$inferInsert;
