import { HasAnyRoles } from 'src/auth/roles.decorator'
import { KnowledgeBaseService } from './knowledge-base.service'
import { Body, Controller, Delete, Get, Param, Post, Put, UploadedFile, UseInterceptors } from '@nestjs/common'
import { Role } from 'src/model/role.enum'
import { PageRequest } from 'src/common/dto/page-request'
import { KnowledgeBase } from './entities/knowledge-base.entity'
import { KnowledgeFile } from './entities/knowledge-file.entity'
import { FileInterceptor } from '@nestjs/platform-express'

@Controller('knowledge')
@HasAnyRoles(Role.Admin, Role.Org)
export class KnowledgeBaseController {
    constructor(private readonly knowledgeBaseService: KnowledgeBaseService) {}

    @Post('/base')
    async allKnowledgeBase(@Body() pageRequest: PageRequest<KnowledgeBase>) {
        return await this.knowledgeBaseService.findAllKnowledgeBase(pageRequest)
    }

    @Put('/base')
    async createKnowledgeBase(@Body() knowledgeBase: KnowledgeBase) {
        return await this.knowledgeBaseService.createKnowledgeBase(knowledgeBase)
    }

    @Put('/base/:id')
    async updateKnowledgeBase(@Body() knowledgeBase: KnowledgeBase) {
        return await this.knowledgeBaseService.updateKnowledgeBase(knowledgeBase)
    }

    @Delete('/base/:id')
    async deleteKnowledgeBase(@Param('id') id: string) {
        return await this.knowledgeBaseService.deleteKnowledgeBase(Number(id))
    }

    @Post('/file')
    async allKnowledgeFile(@Body() pageRequest: PageRequest<KnowledgeFile>) {
        return await this.knowledgeBaseService.fileAllKnowledgeFile(pageRequest)
    }

    @Put('/base/:id/file')
    @UseInterceptors(FileInterceptor('file'))
    public async uploadFile(@UploadedFile() file: Express.Multer.File, @Param('id') id: string) {
        return await this.knowledgeBaseService.uploadKnowledgeFile(file, Number(id))
    }

    @Delete('/file/:id')
    async deleteKnowledgeFile(@Param('id') id: string) {
        return await this.knowledgeBaseService.deleteKnowledgeFile(Number(id))
    }
}
