import { Args, Context, Int, Query, Resolver } from '@nestjs/graphql';
import { Apparatus } from '../models/Apparatus';
import { ApparatusService } from '../apparatus.service';
import { Client } from '../../client';
import { HttpException } from '@nestjs/common';
import { UUID } from '../../global/scalars/UUID';

@Resolver(() => Apparatus)
export class ApparatusResolverQ {
  constructor(
    private readonly service: ApparatusService,
  ) {}

  @Query(() => Apparatus, { nullable: true })
  async Apparatus(
    @Context('client') client: Client,
    @Args('id') id: UUID,
  ): Promise<Apparatus> {
    return await this.service.findOneById(id) as Apparatus;
  }

  @Query(() => [Apparatus], { nullable: true })
  async ApparatusFind(
    @Context('client') client: Client,
    @Args('name', { nullable: true }) name?: string,
    @Args('limit', { type: () => Int, nullable: true }) limit?: number,
    @Args('offset', { type: () => Int, nullable: true }) offset?: number,
  ): Promise<Apparatus[]> {
    const filter: any = {}

    if (name) filter.name = name;

    let tmp = await this.service.find(filter, limit, offset);
    if (tmp.length > 1000) throw new HttpException('too many results', 413);

    return tmp
  }
}
