import { Injectable } from '@nestjs/common';
import { PrismaService } from '../../infrastructure/database/prisma.service';

@Injectable()
export class ActiveRecallRepository {
  constructor(private readonly prisma: PrismaService) {}

  async findByUserId(userId: string, pagination?: { page?: number; limit?: number }) {
    const page = pagination?.page ?? 1;
    const limit = pagination?.limit ?? 20;
    return this.prisma.activeRecallQuestion.findMany({
      where: { userId },
      orderBy: { createdAt: 'desc' },
      skip: (page - 1) * limit,
      take: limit,
    });
  }

  async findById(id: string) {
    return this.prisma.activeRecallQuestion.findUnique({ where: { id } });
  }

  async createQuestion(data: {
    userId: string;
    knowledgeItemId?: string;
    questionText: string;
    difficulty?: string;
    createdBy?: string;
  }) {
    return this.prisma.activeRecallQuestion.create({
      data: {
        userId: data.userId,
        knowledgeItemId: data.knowledgeItemId ?? null,
        questionText: data.questionText,
        difficulty: data.difficulty ?? 'normal',
        createdBy: data.createdBy ?? 'ai',
      },
    });
  }

  async createAnswer(userId: string, questionId: string, body: { answerText: string }) {
    return this.prisma.activeRecallAnswer.create({
      data: {
        userId,
        questionId,
        answerText: body.answerText,
        submittedAt: new Date(),
      },
    });
  }
}
