import { eq } from "drizzle-orm";
import { db } from "../config/database.js";
import {
  questions,
  type Question,
  type NewQuestion,
} from "./questions.schema.js";

export class QuestionsRepository {
  async findById(id: string): Promise<Question | undefined> {
    const [result] = await db
      .select()
      .from(questions)
      .where(eq(questions.id, id))
      .limit(1);
    return result;
  }

  async findByKnowledgePointId(knowledgePointId: string): Promise<Question[]> {
    return db
      .select()
      .from(questions)
      .where(eq(questions.knowledgePointId, knowledgePointId));
  }

  async create(data: NewQuestion): Promise<void> {
    await db.insert(questions).values(data);
  }

  async update(id: string, data: Partial<NewQuestion>): Promise<void> {
    await db.update(questions).set(data).where(eq(questions.id, id));
  }

  async delete(id: string): Promise<void> {
    await db.delete(questions).where(eq(questions.id, id));
  }
}

export const questionsRepository = new QuestionsRepository();
