import {
    Controller,
    Get,
    Post,
    Body,
    Patch,
    Param,
    Delete,
    UseGuards,
    Req,
    Query,
    UseInterceptors,
    UploadedFile,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { CombinedAuthGuard } from '../auth/combined-auth.guard';
import { NoteService } from './note.service';
import { Note } from './note.entity';

@Controller('notes')
@UseGuards(CombinedAuthGuard)
export class NoteController {
    constructor(private readonly noteService: NoteService) { }

    @Post()
    create(@Req() req, @Body() createNoteDto: Partial<Note>) {
        return this.noteService.create(req.user.id, req.user.tenantId, createNoteDto);
    }

    @Get()
    findAll(
        @Req() req,
        @Query('groupId') groupId?: string,
        @Query('categoryId') categoryId?: string,
    ) {
        return this.noteService.findAll(req.user.id, req.user.tenantId, req.user.isAdmin, groupId, categoryId);
    }

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

    @Patch(':id')
    update(
        @Req() req,
        @Param('id') id: string,
        @Body() updateNoteDto: Partial<Note>,
    ) {
        return this.noteService.update(req.user.id, req.user.tenantId, id, updateNoteDto, req.user.isAdmin);
    }

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

    @Post('from-pdf-selection')
    @UseInterceptors(FileInterceptor('screenshot'))
    createFromPDFSelection(
        @Req() req,
        @UploadedFile() screenshot: Express.Multer.File,
        @Body('fileId') fileId: string,
        @Body('groupId') groupId?: string,
        @Body('pageNumber') pageNumber?: string,
    ) {
        return this.noteService.createFromPDFSelection(
            req.user.id,
            req.user.tenantId,
            fileId,
            screenshot,
            groupId,
            pageNumber ? parseInt(pageNumber, 10) : undefined,
        );
    }
}
