import {
  Controller,
  Post,
  Req,
  UseGuards,
  BadRequestException,
  Body
} from '@nestjs/common';
import { UserCreateInput } from '../@generated/prisma-nestjs-graphql';

import { AuthenticationService } from './authentication.service';
import { JwtAuthGuard } from './jwt-auth.guard';
import { LocalAuthGuard } from './local-auth.guard';

@Controller('authentication')
export class AuthenticationController {
  constructor(private readonly $authService: AuthenticationService) {}

  @UseGuards(LocalAuthGuard)
  @Post('login')
  public async login(
    @Req() req: any,
    @Body('allowRefresh') allowRefresh: boolean
  ) {
    return this.$authService.login(req.user, allowRefresh);
  }

  @UseGuards(JwtAuthGuard)
  @Post('register')
  public async register(@Body() data: UserCreateInput) {
    try {
      return this.$authService.register(data);
    } catch (error: any) {
      return new BadRequestException(error.message);
    }
  }

  @UseGuards(JwtAuthGuard)
  @Post('refresh-token')
  public async refreshToken(@Body('accessToken') token: string) {
    return null;
  }
}
