import {
    Controller,
    Put,
    Get,
    Body,
    Param,
    HttpStatus,
    NotFoundException,
    Delete,
    BadRequestException,
    Req,
    Post
} from '@nestjs/common'
import { HasRoles } from '../auth/roles.decorator'
import { Role } from '../model/role.enum'
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'
import { PageRequest } from '../common/dto/page-request'
import { Mask } from './entities/mask.entity'
import { MaskDto } from './dto/mask.dto'
import { MaskService } from './mask.service'

@ApiTags('mask.admin')
@Controller('/admin/mask')
@ApiBearerAuth()
export class MaskAdminController {
    constructor(private readonly maskService: MaskService) { }


    @Post()
    public async list(@Body() page: PageRequest<Mask>) {
        return await this.maskService.findAll(page)
    }

    @Get('/getRandom/:num')
    public async randomQuery(@Param('num') num: number) {
        return await this.maskService.randomQuery(num)
    }


    @Put()
    @HasRoles(Role.Admin)
    public async create(@Body() mask: MaskDto) {
        return await this.maskService.create(mask)
    }

    @Get('/get/:maskId')
    public async get(@Param('maskId') maskId: string) {
        const mask = await this.maskService.findById(Number(maskId))
        return mask
    }

    @Put('/:maskId')
    @HasRoles(Role.Admin)
    public async updateMask(@Param('maskId') maskId: string, @Body() maskDto: MaskDto) {
        try {
            await this.maskService.updateMask(Number(maskId), maskDto)

            return {
                message: 'Mask Updated successfully!',
                status: HttpStatus.OK
            }
        } catch (err) {
            throw new BadRequestException(err, 'Error: Mask not updated!')
        }
    }

    @Delete('/:maskId')
    @HasRoles(Role.Admin)
    public async deleteMask(@Param('maskId') maskId: number) {
        try {
            await this.maskService.deleteMask(maskId)
            return {
                message: 'Mask Deleted successfully!',
                status: HttpStatus.OK
            }
        } catch (err) {
            throw new BadRequestException(err, 'Error: Mask not deleted!')
        }
    }

}
