import AppResponse from '@/response/app-response';
import {
  Body,
  Controller,
  Get,
  Param,
  Post,
  Put,
  Query,
  UploadedFile,
  UseInterceptors,
} from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { FileInterceptor } from '@nestjs/platform-express';
import { plainToClass } from 'class-transformer';
import { memoryStorage } from 'multer';
import { ClientInfoUpdateBidDto } from '../../dto/bid/client-info-update-bid.dto';
import { ClientUpdateBidDto } from '../../dto/bid/client-update-bid.dto';
import { ClientUpdateLoginStatusDto } from '../../dto/bid/client-update-login-status.dto';
import { CreateBidDto } from '../../dto/bid/create-bid.dto';
import { UpdateStatusByPriceDto } from '../../dto/bid/update-status-by-price.dto';
import { Bid } from '../../entities/bid.entity';
import { WebBid } from '../../entities/wed-bid.entity';
import { BidsService } from '../../services/bids.service';
import { WebBidsService } from '../../services/web-bids.service';
import { Event } from '../../utils/events';

@Controller('bids')
export class BidsController {
  constructor(
    private readonly bidsService: BidsService,
    private readonly eventEmitter: EventEmitter2,
    private readonly webBidService: WebBidsService,
  ) {}

  @Get()
  index() {
    return this.bidsService.clientIndex();
  }

  @Get(':model')
  getByModel(@Param('model') model: Bid['model']) {
    return this.bidsService.getBidByModel(model);
  }

  @Post()
  create(@Body() data: CreateBidDto) {
    return this.bidsService.create(data, (result) => !!result);
  }

  @Put(':id')
  update(@Param('id') id: Bid['id'], @Body() data: ClientUpdateBidDto) {
    return this.bidsService.clientUpdate(id, data);
  }

  @Put('info/:id')
  updateInfo(@Param('id') id: Bid['id'], @Body() data: ClientInfoUpdateBidDto) {
    return this.bidsService.update(id, data);
  }

  @Post('out-bid/:id')
  outBidStatus(@Param('id') id: Bid['id']) {
    return this.bidsService.outBid(id);
  }

  @Post('update-status/:id')
  updateStatusByPrice(
    @Param('id') id: Bid['id'],
    @Body() data: UpdateStatusByPriceDto,
  ) {
    return this.bidsService.updateStatusByPrice(id, data);
  }

  @Post('update-status-work/:type/:id')
  @UseInterceptors(FileInterceptor('image', { storage: memoryStorage() }))
  async updateStatusWork(
    @Param('id') id: Bid['id'],
    @Param('type') type: string,
    @UploadedFile() image: Express.Multer.File,
  ) {
    return this.bidsService.updateStatusWork(id, type, image);
  }

  @Post('upload-record/:id')
  @UseInterceptors(FileInterceptor('video', { storage: memoryStorage() }))
  async uploadRecord(
    @Param('id') id: Bid['id'],
    @UploadedFile() video: Express.Multer.File,
  ) {
    return this.bidsService.uploadRecord(id, video);
  }

  @Post('update-login-status')
  async updateLoginStatus(@Body() data: ClientUpdateLoginStatusDto) {
    return await this.bidsService.emitLoginStatus(data);
  }

  @Post('test')
  async test(@Body('code') code: string) {
    const webBid = await this.webBidService.webBidRepo.findOne({
      // where: { id: 4 },
      where: { id: 1 },
    });

    this.eventEmitter.emit(Event.verifyCode(webBid), {
      code,
      // name: 'LAWSONS',
      name: 'LANGTONS',
      web_bid: plainToClass(WebBid, webBid),
    });

    return AppResponse.toResponse({ code });
  }

  @Post('hook-action')
  async hookAction(
    @Body() data: any,
    @Query('id') id: Bid['id'],
    @Query('type') type: 'action' | 'api',
  ) {
    return this.bidsService.hookAction({ id, type }, data);
  }
}
