import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiBody, ApiTags } from '@nestjs/swagger';
import { ResponseMessage } from 'src/common/decorators/response_message.decorator';
import { Roles } from 'src/common/decorators/roles.decorator';
import { CurrentUser } from 'src/common/decorators/user.decorator';
import { JwtAuthGuard } from 'src/guards/jwt-auth.guard';
import { RolesGuard } from 'src/guards/roles.guard';
import { USER_ROLE } from '../users/users.constant';
import { IFullUser } from '../users/users.interface';
import { AuthService } from './auth.service';
import { BulkUserRegisterDto, ChangePasswordDto, ResetPasswordDto, UserLoginDto, UserRegisterDto, UserSocialLoginDto } from './auth.validation';
import { VerificationsService } from './features/verifications/verifications.service';
import { RequestVerificationDto } from './features/verifications/verifications.validation';
import { AUTH_SUCCESS_MESSAGES } from './auth.constant';

@ApiTags('Authentications')
@Controller('auth')
export class AuthController {
    constructor(
        private authService: AuthService,
        private verificationService: VerificationsService
    ) { }

    @Post('login')
    @ApiBody({ type: UserLoginDto })
    @ResponseMessage(AUTH_SUCCESS_MESSAGES.LOGIN)
    async login(
        @Body() body: UserLoginDto
    ) {
        return await this.authService.signIn(body.email, body.password);
    }

    @Get('helf')
    @ResponseMessage('Helf endpoint reached')
    async helf() {
        return { message: 'Helf endpoint is operational' };
    }


    @Post('social-login')
    @ApiBody({ type: UserSocialLoginDto })
    @ResponseMessage(AUTH_SUCCESS_MESSAGES.LOGIN)
    async socialLogin(
        @Body() body: UserSocialLoginDto
    ) {
        return await this.authService.socialLogin(body);
    }



    @Post('register')
    @ApiBody({ type: UserRegisterDto })
    @ResponseMessage(AUTH_SUCCESS_MESSAGES.REGISTER)
    async register(
        @Body() body: UserRegisterDto
    ) {
        return await this.authService.register(body);
    }

    // PASSWORD

    @Post('change-password')
    @ApiBearerAuth()
    @UseGuards(JwtAuthGuard, RolesGuard)
    @Roles(USER_ROLE.admin, USER_ROLE.user, USER_ROLE.super_admin)
    @ResponseMessage(AUTH_SUCCESS_MESSAGES.CHANGE_PASSWORD)
    async changePassword(
        @Body() body: ChangePasswordDto,
        @CurrentUser() user: IFullUser
    ) {
        return await this.authService.changePassword(body, user);
    }

    @Post('forgot-password')
    @ApiBody({ type: RequestVerificationDto })
    @ResponseMessage(AUTH_SUCCESS_MESSAGES.FORGOT_PASSWORD)
    async forgotPassword(
        @Body() body: RequestVerificationDto
    ) {
        return await this.verificationService.requestVerification(body.email, body.type)
    }

    @Post('reset-password')
    @ApiBody({ type: ResetPasswordDto })
    @ResponseMessage(AUTH_SUCCESS_MESSAGES.RESET_PASSWORD)
    async resetPassword(
        @Body() body: ResetPasswordDto
    ) {
        return await this.authService.resetPassword(body);
    }


    @Post('add-bulk-users')
    @ApiBearerAuth()
    @UseGuards(JwtAuthGuard, RolesGuard)
    @Roles(USER_ROLE.super_admin)
    @ResponseMessage(AUTH_SUCCESS_MESSAGES.ADD_BULK_USERS)
    async addBulkUsers(
        @Body() body: BulkUserRegisterDto,
    ) {
        return await this.authService.addBulkUsers(body);
    }
}
