import { eq } from "drizzle-orm";
import { db } from "../config/database.js";
import {
  knowledgePoints,
  type KnowledgePoint,
  type NewKnowledgePoint,
} from "./knowledge-points.schema.js";

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

  async findByChapterId(chapterId: string): Promise<KnowledgePoint[]> {
    return db
      .select()
      .from(knowledgePoints)
      .where(eq(knowledgePoints.chapterId, chapterId));
  }

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

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

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

export const knowledgePointsRepository = new KnowledgePointsRepository();
