import { Args, Context, Int, Query, Resolver } from '@nestjs/graphql';
import { Event } from '../models/Event';
import { EventService } from '../event.service';
import { Client } from '../../client';
import { HttpException } from '@nestjs/common';
import { UUID } from '../../global/scalars/UUID';
import { Date } from '../../global/scalars/Date';

@Resolver(() => Event)
export class EventResolverQ {
  constructor(
    private readonly service: EventService,
  ) {}

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

  @Query(() => [Event], { nullable: true })
  async EventFind(
    @Context('client') client: Client,
    @Args('date', { nullable: true }) date?: Date,
    @Args('organizer', { nullable: true }) organizer?: UUID,
    @Args('limit', { type: () => Int, nullable: true }) limit?: number,
    @Args('offset', { type: () => Int, nullable: true }) offset?: number,
  ): Promise<Event[]> {
    const filter: any = {}

    if (date) filter.date = date;
    if (organizer) filter._organizer = organizer;

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

    return tmp
  }
}
