import { Injectable, NotFoundException } from '@nestjs/common';
<% if (dbType === 'mongoose') { %>
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { <%= modelName %>, <%= modelName %>Document } from './<%= modelName.toLowerCase() %>.schema';
<% } else if (dbType === 'prisma') { %>
import { PrismaService } from '../prisma/prisma.service';
<% } %>
import { Create<%= modelName %>Dto } from './dto/create-<%= modelName.toLowerCase() %>.dto';
import { Update<%= modelName %>Dto } from './dto/update-<%= modelName.toLowerCase() %>.dto';

@Injectable()
export class <%= modelName %>Service {
<% if (dbType === 'mongoose') { %>
  constructor(@InjectModel(<%= modelName %>.name) private readonly model: Model<<%= modelName %>Document>) {}

  async findAll() {
    return this.model.find().exec();
  }

  async findOne(id: string) {
    const doc = await this.model.findById(id).exec();
    if (!doc) throw new NotFoundException(`<%= modelName %> #${id} not found`);
    return doc;
  }

  async create(dto: Create<%= modelName %>Dto) {
    return this.model.create(dto);
  }

  async update(id: string, dto: Update<%= modelName %>Dto) {
    const doc = await this.model.findByIdAndUpdate(id, dto, { new: true, runValidators: true }).exec();
    if (!doc) throw new NotFoundException(`<%= modelName %> #${id} not found`);
    return doc;
  }

  async remove(id: string) {
    const doc = await this.model.findByIdAndDelete(id).exec();
    if (!doc) throw new NotFoundException(`<%= modelName %> #${id} not found`);
    return doc;
  }
<% } else { %>
  constructor(private readonly prisma: PrismaService) {}

  async findAll() {
    return this.prisma.<%= modelName.charAt(0).toLowerCase() + modelName.slice(1) %>.findMany();
  }

  async findOne(id: string) {
    const record = await this.prisma.<%= modelName.charAt(0).toLowerCase() + modelName.slice(1) %>.findUnique({ where: { id } });
    if (!record) throw new NotFoundException(`<%= modelName %> #${id} not found`);
    return record;
  }

  async create(dto: Create<%= modelName %>Dto) {
    return this.prisma.<%= modelName.charAt(0).toLowerCase() + modelName.slice(1) %>.create({ data: dto });
  }

  async update(id: string, dto: Update<%= modelName %>Dto) {
    return this.prisma.<%= modelName.charAt(0).toLowerCase() + modelName.slice(1) %>.update({ where: { id }, data: dto });
  }

  async remove(id: string) {
    return this.prisma.<%= modelName.charAt(0).toLowerCase() + modelName.slice(1) %>.delete({ where: { id } });
  }
<% } %>
}
