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

export const questions = mysqlTable(
  "content_questions",
  {
    id: varchar("id", { length: 32 }).notNull().primaryKey(),
    knowledgePointId: varchar("knowledge_point_id", { length: 32 }).notNull(),
    type: varchar("type", { length: 32 }).notNull(),
    content: text("content").notNull(),
    options: json("options").$type<Record<string, unknown> | null>(),
    answer: text("answer").notNull(),
    explanation: text("explanation"),
    difficulty: tinyint("difficulty").notNull().default(3),
    status: varchar("status", { length: 32 }).notNull().default("draft"),
    source: varchar("source", { length: 32 }).notNull().default("manual"),
    createdBy: varchar("created_by", { length: 32 }).notNull(),
    metadata: json("metadata").$type<Record<string, unknown> | null>(),
    createdAt: timestamp("created_at").notNull().defaultNow(),
    updatedAt: timestamp("updated_at").notNull().defaultNow().onUpdateNow(),
  },
  (table) => ({
    kpIdx: index("idx_questions_kp").on(table.knowledgePointId),
    typeDifficultyIdx: index("idx_questions_type_difficulty").on(
      table.type,
      table.difficulty,
    ),
    statusIdx: index("idx_questions_status").on(table.status),
    sourceIdx: index("idx_questions_source").on(table.source),
  }),
);

export type Question = typeof questions.$inferSelect;
export type NewQuestion = typeof questions.$inferInsert;
