import { Context, Parent, ResolveField, Resolver } from '@nestjs/graphql';
import { Event } from '../models/Event';
import { Client } from '../../client';
import { UUID } from '../../global/scalars/UUID';
import {Date} from '../../global/scalars/Date'
import {Timeslot} from '../models/Timeslot'
import {Organizer} from '../../organizer/models/Organizer'
import {OrganizerService} from '../../organizer/organizer.service'

@Resolver(() => Event)
export class EventResolver {
  @ResolveField(() => UUID, { nullable: false })
  async _id(
    @Context('client') client: Client,
    @Parent() parent: Event
  ): Promise<UUID> {
    return parent._id as UUID;
  }

  @ResolveField(() => String, { nullable: true })
  async name(
    @Context('client') client: Client,
    @Parent() parent: Event
  ): Promise<string> {
    return parent.name;
  }

  @ResolveField(() => Date, { nullable: false })
  async date(
    @Context('client') client: Client,
    @Parent() parent: Event
  ): Promise<Date> {
    return parent.date;
  }

  @ResolveField(() => [Timeslot], { nullable: true })
  async timeslots(
    @Context('client') client: Client,
    @Parent() parent: Event
  ): Promise<Timeslot[]> {
    return parent.timeslots || [];
  }

  @ResolveField(() => UUID, { nullable: false })
  async _organizer(
    @Context('client') client: Client,
    @Parent() parent: Event
  ): Promise<UUID> {
    return parent._organizer;
  }

  @ResolveField(() => Organizer, { nullable: false })
  async organizer(
    @Context('client') client: Client,
    @Parent() parent: Event
  ): Promise<Organizer> {
    const organizerService = new OrganizerService();

    return organizerService.findOneById(parent._organizer);
  }
}