import { BadRequestException, Body, Controller, Post } from '@nestjs/common';
import { AuthService } from './auth.service';
import { SignInDto, SignInResponseDto } from './dtos/sign-in.dto';
import { ApiTags } from '@nestjs/swagger';
import { IsPublic } from '../shared/decorators/auth/is-public.decorator';
import {
  ApiDefaultResponseStatus,
  ApiResponseStatuses,
} from '../shared/decorators/swagger/api-response-statuses.decorator';
import { SignInWithGoogleDto } from './dtos/sign-in-with-google.dto';

@IsPublic()
@ApiTags('Auth')
@Controller('auth')
export class AuthController {
  constructor(private readonly _authService: AuthService) {}

  @Post(['sign-in', 'login'])
  @ApiDefaultResponseStatus(200, SignInResponseDto)
  @ApiResponseStatuses(400, 401, 500, 502)
  async signIn(@Body() signInDto: SignInDto): Promise<SignInResponseDto> {
    const { username, email } = signInDto;

    if (!username && !email) throw new BadRequestException();

    return this._authService.signIn(signInDto);
  }

  @Post(['sign-in/google', 'login/google'])
  @ApiDefaultResponseStatus(200, SignInResponseDto)
  @ApiResponseStatuses(400, 403, 500, 502)
  async signInWithGoogle(
    @Body() signInWithGoogleDto: SignInWithGoogleDto,
  ): Promise<SignInResponseDto> {
    return this._authService.signInWithGoogle(signInWithGoogleDto);
  }
}
