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

@Resolver(() => Organizer)
export class OrganizerResolverQ {
  constructor(
    private readonly service: OrganizerService,
  ) {}

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

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

    if (name) filter.name = name;
    if (plz) filter.plz = plz;
    if (ort) filter.ort = ort;

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

    return tmp
  }
}
