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

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

import { QueueAdminOrchestrator } from '../../domain/queue-admin.orchestrator';
import { PaginationResponse } from 'src/core/response/domain/ok-response.interface';
import { Queue } from '../../domain/entities/queue.entity';
import { QueueDto } from './dto/queue.filter';
import { Pagination } from 'src/core/response';
import { ExcludePrivilege, Public } from 'src/core/guards';
import { UserProvider } from 'src/core/sessions';

@ApiTags(`Queue Admin`)
@Controller(`v1/${MODULE_NAME.QUEUE}-admin`)
@ApiBearerAuth('JWT')
@Public(false)
@ExcludePrivilege()
export class QueueAdminController {
  constructor(
    private orchestrator: QueueAdminOrchestrator,
    private readonly userProvider: UserProvider,
  ) {}

  @Get('queues')
  @Pagination()
  async index(@Query() params: QueueDto): Promise<PaginationResponse<Queue>> {
    const queue_id = this.userProvider.user.item_id;
    if (!queue_id) {
      throw new UnauthorizedException({
        error: 'USER_IS_NOT_ADMIN',
        message: "Login user isn't admin, please login in admin account",
        code: 20001,
      });
    }
    return await this.orchestrator.index({ ...params, queue_id });
  }

  @Get('queues/:id')
  @Public(true)
  @Pagination()
  async indexPublic(
    @Param('id') id: string,
    @Query() params: QueueDto,
  ): Promise<PaginationResponse<Queue>> {
    return await this.orchestrator.index({ ...params, queue_id: id });
  }

  @Post('queues/:id/call')
  async call(@Param('id') id: string) {
    return await this.orchestrator.call(id);
  }

  @Post('queues/:id/done')
  async done(@Param('id') id: string) {
    return await this.orchestrator.done(id);
  }
}
