import { Service } from '@athenna/ioc'
import { {{ crudNamePascal }} } from '{{ modelImportPath }}'
import { NotFoundException } from '@athenna/http'
import type { PaginationOptions } from '@athenna/common'

@Service()
export class {{ crudNamePascal }}Service {
  public async paginate(options?: PaginationOptions) {
    return {{ crudNamePascal }}.paginate(options)
  }

  public async create(body: Partial<{{ crudNamePascal }}>) {
    return {{ crudNamePascal }}.create(body)
  }

  public async findById(id: string, trashed = false) {
    const query = {{ crudNamePascal }}.query()
    const {{ crudNameLower }} = await query
      .when(trashed, () => query.withTrashed())
      .where({ id })
      .find()

    if (!{{ crudNameLower }}) {
      throw new NotFoundException('{{ crudNamePascal }} not found')
    }

    return {{ crudNameLower }}
  }

  public async update(id: string, body: Partial<{{ crudNamePascal }}>) {
    const {{ crudNameLower }} = await this.findById(id)

{{{ propertiesToUpdate }}}
    return {{ crudNameLower }}.save()
  }

  public async delete(id: string) {
    const {{ crudNameLower }} = await this.findById(id, true)

    await {{ crudNameLower }}.delete()
  }
}

