import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  ParseIntPipe,
  Post,
  Put,
  Req,
  UseGuards,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import AuthGuard, { AllowUnauthorizedRequest } from '../../lib/auth.guard';
import {
  CreateCanvasDto,
  CreateSavedQueryDto,
  PublishCanvasDto,
  UpdateCanvasDto,
} from './canvas.dto';
import { CanvasService } from './canvas.service';

@ApiTags('canvas')
@UseGuards(AuthGuard)
@Controller('canvas')
export class CanvasController {
  constructor(private readonly canvasService: CanvasService) {}

  @Get()
  findAll(@Req() req) {
    return this.canvasService.findAllByUser(req.user.userid);
  }

  @Post()
  create(@Req() req, @Body() dto: CreateCanvasDto) {
    return this.canvasService.create(req.user.userid, dto);
  }

  @AllowUnauthorizedRequest()
  @Post('saved-query')
  savedQueryCreate(@Body() dto: CreateSavedQueryDto) {
    return this.canvasService.savedQueryCreate(dto);
  }

  @AllowUnauthorizedRequest()
  @Get('saved-query/:id/run')
  savedQueryRun(@Param('id', ParseIntPipe) id: number) {
    return this.canvasService.savedQueryRun(id);
  }

  @AllowUnauthorizedRequest()
  @Get('by-page-code/:slug')
  findByPageCode(@Param('slug') slug: string) {
    return this.canvasService.findByPageCode(slug);
  }

  @Get(':id')
  findOne(@Req() req, @Param('id') id: string) {
    return this.canvasService.findOne(id, req.user.userid);
  }

  @Put(':id')
  update(@Req() req, @Param('id') id: string, @Body() dto: UpdateCanvasDto) {
    return this.canvasService.update(id, req.user.userid, dto);
  }

  @Delete(':id')
  remove(@Req() req, @Param('id') id: string) {
    return this.canvasService.remove(id, req.user.userid);
  }

  @Post(':id/publish')
  publish(@Req() req, @Param('id') id: string, @Body() dto: PublishCanvasDto) {
    return this.canvasService.publish(id, req.user.userid, dto);
  }

  @Delete(':id/unpublish')
  unpublish(@Req() req, @Param('id') id: string) {
    return this.canvasService.unpublish(id, req.user.userid);
  }
}
