import {
  Body,
  Controller,
  Post,
  Put,
  Request,
  UseGuards,
} from '@nestjs/common';
import { AuthService } from './auth.service';
import { LocalAuthGuard } from './guards/local-auth.guard';
import { CreateUserDto } from '../users/dto/create-user.dto';
import { JwtAuthGuard } from './guards/jwt-auth.guard';
import { UpdateUserDto } from '../users/dto/update-user.dto';
import { ResetPasswordDto } from '../users/dto/reset-password.dto';
import { ChangePasswordDto } from '../users/dto/change-password.dto';

@Controller({
  path: 'auth',
  version: '1',
})
export class AuthController {
  constructor(private authService: AuthService) {}

  @UseGuards(LocalAuthGuard)
  @Post('sign-in')
  async singIn(@Request() req) {
    return this.authService.signIn(req.user);
  }

  @Post('sign-up')
  async singUp(@Body() createUser: CreateUserDto) {
    return this.authService.signUp(createUser);
  }

  @Post('reset-password')
  async resetPassword(@Body() body: ResetPasswordDto) {
    return this.authService.resetPassword(body);
  }

  @Post('init-reset-password-by-mail')
  async initResetPasswordByEmail(
    @Request() req,
    @Body() body: { email: string },
  ) {
    return this.authService.initResetPassword(body);
  }

  @Post('check-email')
  async checkEmail(@Body() body: { email: string }) {
    return this.authService.checkEmail(body);
  }

  @UseGuards(JwtAuthGuard)
  @Post('change-password')
  async changePassword(@Request() req, @Body() body: ChangePasswordDto) {
    return this.authService.changePassword(body, req.user);
  }

  @UseGuards(JwtAuthGuard)
  @Put('update')
  update(@Request() req, @Body() newUser: UpdateUserDto) {
    return this.authService.update(req.user, newUser);
  }
}
