import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Query,
  UploadedFile,
  UseGuards,
  UseInterceptors,
} from '@nestjs/common';
import { ApiBearerAuth, ApiConsumes, ApiTags } from '@nestjs/swagger';
import { MenuService } from './menu.service';
import { CreateMenuDto, GetMenuDto, QueryMenuDto, UpdateMenuDto, UploadCoverDto } from './dto';
import { JwtGuard } from '@/common/guards';
import { FileInterceptor } from '@nestjs/platform-express';
import { diskStorage } from 'multer';
import { extname } from 'path';
import { OSSService } from '../oss/oss.service';
import { ConfigService } from '@nestjs/config';

@Controller('menu')
@ApiTags('menu')
@ApiBearerAuth('JWT')
@UseGuards(JwtGuard)
export class MenuController {
  constructor(
    private readonly menuService: MenuService,
    private readonly oSSService: OSSService,
    private readonly configService: ConfigService,
  ) {}

  @Post()
  create(@Body() createMenuDto: CreateMenuDto) {
    return this.menuService.create(createMenuDto);
  }

  @Get()
  getAll(@Param() getMenuDto: GetMenuDto) {
    return this.menuService.findAll(getMenuDto);
  }

  @Get('detail/:id')
  getMenuDetail(@Param('id') id: string) {
    return this.menuService.findWithChild(+id);
  }

  @Get('page')
  findPagination(@Query() queryDto: QueryMenuDto) {
    return this.menuService.findPagination(queryDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.menuService.remove(+id);
  }
  @Patch(':id')
  update(@Param('id') id: string, @Body() updateMenuDto: UpdateMenuDto) {
    return this.menuService.update(+id, updateMenuDto);
  }

  @Get('level/:id')
  getLevelMenu(@Param('id') id: string) {
    return this.menuService.findLevel(+id);
  }

  @Post('cover/upload')
  @ApiConsumes('multipart/form-data')
  @UseInterceptors(FileInterceptor('file'))
  // @UseInterceptors(
  //   FileInterceptor('file', {
  //     storage: diskStorage({
  //       destination: './uploads',
  //       filename: (req, file, cb) => {
  //         // Generating a 32 random chars long string
  //         const randomName = Array(32)
  //           .fill(null)
  //           .map(() => Math.round(Math.random() * 16).toString(16))
  //           .join('');
  //         //Calling the callback passing the random name generated with the original extension name
  //         cb(null, `${randomName}${extname(file.originalname)}`);
  //       },
  //     }),
  //   }),
  // )
  async updateCover(@Body() data: UploadCoverDto, @UploadedFile() file) {
    const result = await this.oSSService.upload(file, {
      filepath: this.configService.get('OSS_FOLDER'),
    });
    if (result && result.length > 0) {
      return this.configService.get('OSS_DOMAIN') + '/' + result[0].path;
    } else {
      return false;
    }
  }
}
