import { Body, Controller, Get, Param, Post, Query, Res } from '@nestjs/common';

import { MODULE_NAME } from 'src/core/strings/constants/module.constants';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';

import { Public } from 'src/core/guards';
import { QueueOrchestrator } from '../../domain/queue.orchestrator';
import { QueueOrder } from '../../domain/entities/order.entity';
import { Queue } from '../../domain/entities/queue.entity';
import { RegisterQueueDto } from './dto/register-queue.dto';
import { SplitQueueDto } from './dto/split-queue.dto';
import { LoginQueueDto } from './dto/login-queue.dto';
import { LoginReceiptDto } from './dto/login-receipt.dto';
import { mappingRevertTransaction } from 'src/modules/transaction/transaction/domain/usecases/managers/helpers/mapping-transaction.helper';
import { TransactionType } from 'src/modules/transaction/transaction/constants';
import { QueueModel } from '../../data/models/queue.model';

import * as Gtts from 'gtts';
import { Response } from 'express';

@ApiTags(`Queue`)
@Controller(`v1/${MODULE_NAME.QUEUE}`)
@Public(true)
@ApiBearerAuth('JWT')
export class QueueController {
  constructor(private orchestrator: QueueOrchestrator) {}

  @Post('generate')
  async generateQueue(@Body() data: any): Promise<QueueModel[]> {
    mappingRevertTransaction(data, TransactionType.COUNTER);
    return await this.orchestrator.generate(data);
  }

  @Post('register')
  async registerQueue(@Body() data: RegisterQueueDto): Promise<Queue> {
    return await this.orchestrator.create(data);
  }

  @Post('split')
  async splitQueue(@Body() data: SplitQueueDto): Promise<QueueOrder> {
    return await this.orchestrator.split(data);
  }

  @Post('login')
  async loginQueue(@Body() data: LoginQueueDto): Promise<QueueOrder> {
    return await this.orchestrator.loginPhone(data);
  }

  @Post('login/receipt')
  async loginReceipt(@Body() { id }: LoginReceiptDto): Promise<QueueOrder> {
    console.log(id);
    return await this.orchestrator.loginCustomer(id);
  }

  @Get('login/:id')
  async loginCustomer(@Param('id') id: string): Promise<QueueOrder> {
    return await this.orchestrator.loginCustomer(id);
  }

  @Get(':id/tickets')
  async queueTickets(@Param('id') id: string): Promise<void> {
    return await this.orchestrator.queueTickets(id);
  }

  @Get(':id/tickets/:ticket_id/items')
  async queueTicketItems(
    @Param('id') id: string,
    @Param('ticket_id') ticket_id: string,
  ): Promise<void> {
    return await this.orchestrator.queueTicketDetail(id, ticket_id);
  }

  @Get(':id/tickets/:ticket_id/summary')
  async queueTicketActive(
    @Param('id') id: string,
    @Param('ticket_id') ticket_id: string,
  ): Promise<void> {
    return await this.orchestrator.queueTicketSummary(id, ticket_id);
  }

  @Get(':id/recommendations')
  async queueItemRecommends(@Param('id') id: string): Promise<void> {
    return await this.orchestrator.queueItemRecommendation(id);
  }

  @Get(':id/queue-summary')
  async queueItemSummary(): Promise<void> {
    return await this.orchestrator.queueItemSummary();
  }

  @Get(':id/items')
  async queueItems(@Param('id') id: string): Promise<void> {
    return await this.orchestrator.queueItems(id);
  }

  @Get(':id/unique-items')
  async queueUniqueItems(@Param('id') id: string): Promise<void> {
    return await this.orchestrator.queueUniqueItems(id);
  }

  @Get(':id/pos-items')
  async queuePosItems(@Param('id') id: string): Promise<void> {
    return await this.orchestrator.queuePOSTickets(id);
  }

  @Get(':id/queue')
  async queueOrderItems(@Param('id') id: string): Promise<void> {
    return await this.orchestrator.queueOrderItems(id);
  }

  @Get('speech')
  async speech(
    @Query('text') text: string,
    @Res() response: Response,
  ): Promise<void> {
    const gtts = new Gtts(text, 'id');
    gtts.stream().pipe(response);
  }

  @Get(':id/items/:item_id')
  async queueItemTickets(
    @Param('id') id: string,
    @Param('item_id') item_id: string,
  ): Promise<void> {
    return await this.orchestrator.queueItemDetail(id, item_id);
  }
}
