import { Body, Controller, Delete, Get, Param, Patch, Post, Put, Query, Req } from '@nestjs/common'
import { PaperService } from './paper.service'
import { PageRequest } from '../common/dto/page-request'
import { PaperOrder } from './entities/paper-order.entity'
import { PaperGenResult } from './entities/paper-gen-result.entity'
import { CreatePaperOrderDto } from './dto/create-order.dto'
import { HasAnyRoles } from 'src/auth/roles.decorator'
import { PaperOrderAttachment } from './entities/paper-order-attachment.entity'

@Controller('paper')
@HasAnyRoles('paperOrder', 'paperGen')
export class PaperController {
    constructor(private readonly paperService: PaperService) {}

    @Post('/orders')
    async orders(@Body() page: PageRequest<PaperOrder>, @Req() req) {
        return await this.paperService.findAllOrders(req, page)
    }

    @Post('/results')
    async results(@Body() page: PageRequest<PaperGenResult>) {
        return await this.paperService.findAllResults(page)
    }

    @Put('/orders')
    async createOrder(@Body() dto: CreatePaperOrderDto, @Req() req) {
        dto.userId = req.user.id
        return await this.paperService.createOrder(dto)
    }

    @Get('/orders/:id')
    async getOrder(@Param('id') id: string) {
        return await this.paperService.findOrderById(Number(id))
    }

    @Patch('/orders/:id')
    async updateOrder(@Param('id') id: string, @Body() data) {
        return await this.paperService.updateOrder(Number(id), data)
    }

    @Post('/orders/:id/gen')
    async gen(@Param('id') id: string) {
        return await this.paperService.gen(Number(id))
    }

    @Post('/orders/genChapters')
    async genChapters(@Body() body) {
        return await this.paperService.genChapters(body.id, body.description)
    }

    @Post('/attachments')
    async attachments(@Body() page: PageRequest<PaperOrderAttachment>) {
        return await this.paperService.attachments(page)
    }

    @Put('/attachments')
    async createAttachment(@Body() dto: PaperOrderAttachment) {
        return await this.paperService.createAttachment(dto)
    }

    @Delete('/attachments/:id')
    async deleteAttachment(@Param('id') id: string) {
        return await this.paperService.deleteAttachment(Number(id))
    }
}
