import {
  Controller,
  Get,
  Param,
  Query,
  UnauthorizedException,
} from '@nestjs/common';
import { FilterItemQueueDto } from './dto/filter-item-queue.dto';
import { Pagination } from 'src/core/response';
import { PaginationResponse } from 'src/core/response/domain/ok-response.interface';
import { ItemQueueEntity } from '../domain/entities/item-queue.entity';
import { ItemQueueReadOrchestrator } from '../domain/usecases/item-queue-read.orchestrator';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { MODULE_NAME } from 'src/core/strings/constants/module.constants';
import { Public } from 'src/core/guards';
import { validate as isValidUUID } from 'uuid';

@ApiTags(`${MODULE_NAME.ITEM_QUEUE.split('-').join(' ')} - read`)
@Controller(`v1/${MODULE_NAME.ITEM_QUEUE}`)
@Public(false)
@ApiBearerAuth('JWT')
export class ItemQueueReadController {
  constructor(private orchestrator: ItemQueueReadOrchestrator) {}

  @Get()
  @Pagination()
  async index(
    @Query() params: FilterItemQueueDto,
  ): Promise<PaginationResponse<ItemQueueEntity>> {
    return await this.orchestrator.index(params);
  }

  @Get('list')
  @Public(true)
  async list() {
    const list = await this.orchestrator.list();
    return list.map(({ id, name, item_type }) => {
      return {
        id,
        name,
        item_type,
      };
    });
  }

  @Get(':id')
  async detail(@Param('id') id: string): Promise<ItemQueueEntity> {
    return await this.orchestrator.detail(id);
  }

  @Get('display/:id')
  @Public(true)
  async detailPublic(@Param('id') id: string): Promise<ItemQueueEntity> {
    if (!isValidUUID(id)) throw new UnauthorizedException('id is required');
    return await this.orchestrator.detail(id);
  }
}
