import {CommandBus, EventsHandler, IEventHandler, QueryBus} from '@nestjs/cqrs';
import {UserRegisteredEvent} from '../../game/events/user-registered.event';
import {StateService} from '../../state/state.service';
import {GameStateEnum} from '../../state/game-state.enum';
import {CardsService} from '../cards.service';
import {Logger} from '@nestjs/common';
import {ChatgptRequestQuery} from "../../openai/commands/chatgpt-request.command";
import {Case, NameCasesHelper} from "../../openai/helpers/name-cases.helper";
import {SetGuestPropertyCommand} from "../../guests/command/set-guest-property.command";
import * as translit from 'latin-to-cyrillic';
import {GuestPropertiesConsts} from "../../schemas/properties.consts";
import {GetGuestPropertyQuery} from "../../guests/command/get-guest-property.handler";

@EventsHandler(UserRegisteredEvent)
export class CardsUserRegisteredEventHandler implements IEventHandler<UserRegisteredEvent> {
  private readonly logger = new Logger(CardsUserRegisteredEventHandler.name);
  constructor(
    private stateService: StateService,
    private cardsService: CardsService,
    private queryBus: QueryBus,
    private commandBus: CommandBus,
  ) {}
  async handle(event: UserRegisteredEvent): Promise<any> {
    const state = await this.stateService.getState('main');
    if(event.name !=='Elene') {
      const asyncGenitive =  this.queryBus.execute(new ChatgptRequestQuery(NameCasesHelper.getRequestForNameInCase(translit(event.name), Case.GenitiveCase)));
      const asyncAccusative =  this.queryBus.execute(new ChatgptRequestQuery(NameCasesHelper.getRequestForNameInCase(translit(event.name), Case.AccusativeCase)));
      const asyncSubjective =  this.queryBus.execute(new ChatgptRequestQuery(NameCasesHelper.getRequestForNameInCase(translit(event.name), Case.SubjectiveCase)));
      await this.commandBus.execute(new SetGuestPropertyCommand(event.telegramId, GuestPropertiesConsts.NameGenitiveCase, (await asyncGenitive).message.content));
      await this.commandBus.execute(new SetGuestPropertyCommand(event.telegramId, GuestPropertiesConsts.NameAccusativeCase,(await asyncAccusative).message.content));
      await this.commandBus.execute(new SetGuestPropertyCommand(event.telegramId, GuestPropertiesConsts.NameSubjectiveCase,(await asyncSubjective).message.content));
    } else {
      await this.commandBus.execute(new SetGuestPropertyCommand(event.telegramId, GuestPropertiesConsts.NameGenitiveCase, 'Элене'));
      await this.commandBus.execute(new SetGuestPropertyCommand(event.telegramId, GuestPropertiesConsts.NameAccusativeCase,'Элене'));
      await this.commandBus.execute(new SetGuestPropertyCommand(event.telegramId, GuestPropertiesConsts.NameSubjectiveCase,'Элене'));
    }

    this.logger.verbose(`Handle user reg event, game state is: ${state.value}`);

    if (state.value === GameStateEnum.onboarding) {
      this.logger.verbose(`Calling cardService to deal startup cards for ${event.telegramId}`);
      return await this.cardsService.dealStartCards(event.telegramId);
    }
  }
}
