import { PhoneLoginDto } from './dto/login.dto'
import { Body, Controller, Get, Param, Post, Req } from '@nestjs/common'
import { AuthService } from './auth.service'
import { ApiTags } from '@nestjs/swagger'
import { Public } from './public.decorator'
import { HasRoles } from './roles.decorator'
import { Role } from '../model/role.enum'

@ApiTags('auth')
@Controller('/auth')
export class AuthController {
    constructor(private readonly authService: AuthService) {}

    @Public()
    @Post('/phoneLogin')
    async phoneLogin(@Body() loginDto: PhoneLoginDto) {
        return await this.authService.loginByPhone(loginDto)
    }

    @Public()
    @Post('/admin/login')
    async login(@Body() { username, password }) {
        return await this.authService.loginAdmin(username, password)
    }

    @Get('/admin/getRole')
    async getRole(@Req() req) {
        if (req.user.roles.includes(Role.Api)) {
            return 'api'
        }
        return 'admin'
    }

    @Get('/admin/user/:userId/token')
    @HasRoles(Role.Admin)
    async getToken(@Param('userId') userId: string) {
        return await this.authService.getToken(Number(userId))
    }
}
