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

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

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

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

  async create(data: {
    userId: string;
    title: string;
    reason?: string;
    suggestion?: string;
    priority?: string;
    status?: string;
    source?: string;
    knowledgeBaseId?: string;
    knowledgeItemId?: string;
  }) {
    return this.prisma.focusItem.create({
      data: {
        userId: data.userId,
        title: data.title,
        reason: data.reason ?? '',
        suggestion: data.suggestion ?? '',
        priority: data.priority ?? 'normal',
        status: data.status ?? 'open',
        source: data.source ?? null,
        knowledgeBaseId: data.knowledgeBaseId ?? null,
        knowledgeItemId: data.knowledgeItemId ?? null,
      },
    });
  }

  async update(id: string, data: Record<string, any>) {
    return this.prisma.focusItem.update({ where: { id }, data });
  }
}
