import { PrismaClient } from "@prisma/client"

export abstract class BaseRepository<T, CreateInput, UpdateInput> {
  protected prisma: PrismaClient

  constructor(prisma: PrismaClient) {
    this.prisma = prisma
  }

  protected abstract get model(): any

  async create(data: CreateInput): Promise<T> {
    return await this.model.create({ data })
  }

  async findById(id: string): Promise<T | null> {
    return await this.model.findUnique({ where: { id } })
  }

  async findAll(options?: {
    skip?: number
    take?: number
    where?: any
    orderBy?: any
    include?: any
  }): Promise<T[]> {
    return await this.model.findMany(options)
  }

  async update(id: string, data: UpdateInput): Promise<T> {
    return await this.model.update({
      where: { id },
      data,
    })
  }

  async delete(id: string): Promise<T> {
    return await this.model.delete({ where: { id } })
  }

  async count(where?: any): Promise<number> {
    return await this.model.count({ where })
  }

  async upsert(params: { where: any; create: CreateInput; update: UpdateInput }): Promise<T> {
    return await this.model.upsert(params)
  }
}
