import {Context, Int, Parent, ResolveField, Resolver} from '@nestjs/graphql';
import { Organizer } from '../models/Organizer';
import { Client } from '../../client';
import { UUID } from '../../global/scalars/UUID';
import {Person} from '../../person/models/Person'
import {PersonService} from '../../person/person.service'

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

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

  @ResolveField(() => Int, { nullable: true })
  async plz(
    @Context('client') client: Client,
    @Parent() parent: Organizer
  ): Promise<number> {
    return parent.plz;
  }

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

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

  @ResolveField(() => [UUID], { nullable: true })
  async _admins(
    @Context('client') client: Client,
    @Parent() parent: Organizer
  ): Promise<UUID[]> {
    if (!client.isMaster() && !client.isOrganizer(parent._id)) return null;

    return parent._admins;
  }

  @ResolveField(() => [Person], { nullable: true })
  async admins(
    @Context('client') client: Client,
    @Parent() parent: Organizer
  ): Promise<Person[]> {
    if (!client.isMaster() && !client.isOrganizer(parent._id)) return null;

    const personService = new PersonService();

    return Promise.all((parent._admins || []).map(e => personService.findOneById(e)));
  }

  @ResolveField(() => [UUID], { nullable: true })
  async _organizers(
    @Context('client') client: Client,
    @Parent() parent: Organizer
  ): Promise<UUID[]> {
    if (!client.isMaster() && !client.isOrganizer(parent._id)) return null;

    return parent._organizers;
  }

  @ResolveField(() => [Person], { nullable: true })
  async organizers(
    @Context('client') client: Client,
    @Parent() parent: Organizer
  ): Promise<Person[]> {
    if (!client.isMaster() && !client.isOrganizer(parent._id)) return null;

    const personService = new PersonService();

    return Promise.all((parent._organizers || []).map(e => personService.findOneById(e)));
  }

  @ResolveField(() => [UUID], { nullable: true })
  async _pending(
    @Context('client') client: Client,
    @Parent() parent: Organizer
  ): Promise<UUID[]> {
    if (!client.isMaster() && !client.isAdmin(parent._id)) return null;

    return parent._pending;
  }

  @ResolveField(() => [Person], { nullable: true })
  async pending(
    @Context('client') client: Client,
    @Parent() parent: Organizer
  ): Promise<Person[]> {
    if (!client.isMaster() && !client.isAdmin(parent._id)) return null;

    const personService = new PersonService();

    return Promise.all((parent._pending || []).map(e => personService.findOneById(e)));
  }
}