import {
    Controller,
    Put,
    Body,
    Post,
    Delete,
    Param,
    HttpStatus,
    BadRequestException,
    Req
} from '@nestjs/common'
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'
import { CommentService } from './comment.service'
import { Comment } from './entities/comment.entity'
import { PageRequest } from '../common/dto/page-request'
import { CommentDto } from './dto/comment.dto'
import { LikesDto } from 'src/likes/dto/likes.dto'

@ApiTags('comment')
@Controller('/comment')
@ApiBearerAuth()
export class CommentController {

    constructor(private readonly commentService: CommentService) { }

    @Post()
    public async list(@Req() req, @Body() page: PageRequest<Comment>) {
        return await this.commentService.findAll(page, req.user.userId)
    }

    @Post('/childComments')
    public async childList(@Body() page: PageRequest<Comment>) {
        return await this.commentService.findChildList(page)
    }

    @Put()
    public async create(@Body() commentDto: CommentDto) {
        return await this.commentService.create(commentDto)
    }

    @Delete('/:id')
    public async deleteComment(@Param('id') id: number) {
        try {
            await this.commentService.delete(id)
            return {
                message: 'Comment Deleted successfully!',
                status: HttpStatus.OK
            }
        } catch (err) {
            throw new BadRequestException(err, 'Error: Comment not deleted!')
        }
    }


    @Post('/like')
    public async likes(@Body() likesDto: LikesDto) {
        try {
            await this.commentService.like(likesDto.targetId, likesDto.userId)
            return {
                message: 'successes!',
                status: HttpStatus.OK
            }
        } catch (err) {
            throw new BadRequestException(err, 'Error: false!')
        }
    }

}