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

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

  @Mutation(() => Event, { nullable: false })
  async EventCreate(
    @Context('client') client: Client,
    @Args('organizer') organizer: UUID,
    @Args('date') date: Date,
    @Args('name', { nullable: true }) name?: string,
  ): Promise<Event> {
    if (!client.isMaster() && !client.isOrganizer(organizer)) {
      throw new HttpException('You\'re not allowed to create an Event for this organizer', 403);
    }

    const organizerService = new OrganizerService();
    const o = await organizerService.findOneById(organizer);

    if (!o) {
      throw new HttpException('Organizer-ID not found!', 404);
    }

    return this.service.create(client, date, organizer, name);
  }
}
