import { BadRequestException, Body, Controller, Get, HttpStatus, Param, Post, Put, Req } from "@nestjs/common";
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";
import { MomentsService } from "./moments.service";
import { PageRequest } from "../common/dto/page-request";
import { Moments } from "./entities/moments.entity";
import { LikesDto } from "src/likes/dto/likes.dto";
import { HasRoles } from "src/auth/roles.decorator";
import { Role } from "src/model/role.enum";

@ApiTags('moments')
@Controller('/moments')
@ApiBearerAuth()
export class MomentsController {

    constructor(private readonly momentsService: MomentsService) { }

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

    @Get('/get/:id')
    public async get(@Param('id') id: string, @Req() req) {
        try {
            return await this.momentsService.findById(Number(id), req.user.userId)
        } catch (err) {
            throw new BadRequestException(err, 'Error: Moments not existent!')
        }
    }

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

    @Put('/creat')
    @HasRoles(Role.Admin)
    public async manualCreate() {
        await this.momentsService.automaticallySendMoments()
        setTimeout(() => {
            this.momentsService.delay()
        }, 60 * 1000)

    }

}