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 { ChatRole } from './entities/chat-role.entity'
import { ChatRoleDto } from './dto/chat-role.dto'
import { ChatRoleService } from './chat-role.service'
import { Public } from 'src/auth/public.decorator'

@ApiTags('chatRole')
@Controller('/chatRole')
@ApiBearerAuth()
export class ChatRoleController {

    constructor(private readonly chatRoleService: ChatRoleService) { }


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

    @Post('/findByPlayName')
    public async findByPlayName(@Body() chatRole: ChatRole) {
        return await this.chatRoleService.findByPlayerName(chatRole.playerName)
    }

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

    @Put()
    @HasRoles(Role.Admin)
    public async create(@Body() chatRole: ChatRoleDto) {
        return await this.chatRoleService.create(chatRole)
    }

    @Get('/get/:id')
    public async get(@Param('id') id: string) {
        const chatRole = await this.chatRoleService.findById(Number(id))
        return chatRole
    }

    @Put('/:id')
    @HasRoles(Role.Admin)
    public async update(@Param('id') id: string, @Body() chatRoleDto: ChatRoleDto) {
        try {
            await this.chatRoleService.update(Number(id), chatRoleDto)

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

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

}
