import { Controller, Request, Post, Body, Get, Res, UseGuards, UseInterceptors, UploadedFile, Patch, Query, Delete, Put, Req, Param } from '@nestjs/common';
import { ApiOperation, ApiQuery, ApiResponse, ApiTags, ApiBody, ApiConsumes, ApiBearerAuth } from '@nestjs/swagger';
import { AuthService } from './auth.services';
import { toJsonResponse } from '../../common/response';
import { FileInterceptor } from '@nestjs/platform-express';
import { BrandGuard } from '../auth/guards/brand.guard';
import { CommonGuard } from './guards/common.guard';

// DTO Files
import { UserLoginDto } from "./dtos/user-login.dto";
import { UserLogoutDto } from './dtos/user-logout.dto';
import { request } from 'http';
import { UserSocialLoginDto } from './dtos/user-social-login.dto';
import { response } from 'express';
import { UserRegisterDto } from './dtos/user-register.dto';
import { VerifyOtpDto } from './dtos/verify-otp.dto';
import { ForgotPasswordDto } from './dtos/forgot-password.dto';
import { ResetPasswordDto } from './dtos/reset-password.dto';
import { SocialLoginDto } from './dtos/social-login.dto';
import { UserSlugDto } from './dtos/user-slug.dto';
import { ChangePasswordDto } from './dtos/change-password.dto';
import { SocialLoginUpdateDto } from './dtos/social-login-update.dto';
import { UserDetailsQueryDto } from './dtos/user-details-query.dto';
import { AdminGuard } from './guards/admin.guard';
import { VerifyWhatsAppDto } from './dtos/veridy-whatsapp.dto';

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

    // JP-
    @ApiBody({ type: UserRegisterDto })
    @Post('register')
    async userRegister(@Body() request: UserRegisterDto, @Res() response: any) {
        try {
            const data = await this.authService.userRegister(request);
            response.status(201).json(data);
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }

    // JP-
    @ApiBody({ type: VerifyOtpDto })
    @Post('verify')
    async verifyOtp(@Body() request: VerifyOtpDto, @Res() response: any) {
        try {
            const data = await this.authService.verifyOtp(request);
            response.status(200).json(data);
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }


    // JP-
    @ApiBody({ type: UserLoginDto })
    @Post('login')
    async userLogin(@Body() request: UserLoginDto, @Res() response: any) {
        try {
            const data = await this.authService.userLogin(request);
            response.status(200).json(data);
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }

    // JP-
    @ApiBody({ type: SocialLoginDto })
    @Post('social-login')
    async socialLogin(@Body() request: SocialLoginDto, @Res() response: any) {
        try {
            const data = await this.authService.socialLogin(request);
            response.status(200).json(data);
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }

    @ApiBody({ type: SocialLoginUpdateDto })
    @Put('social-login')
    @UseGuards(CommonGuard)
    async socialLoginUpdate(@Body() request: SocialLoginUpdateDto, @Res() response: any, @Req() req: any) {
        try {
            const data = await this.authService.socialLoginUpdate(request, req.user);
            response.status(201).json(data);
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }



    @ApiBearerAuth('access-token')
    @ApiBody({ type: ChangePasswordDto })
    @UseGuards(CommonGuard)
    @Post('change-password')
    async changePassword(@Body() request: ChangePasswordDto, @Request() req, @Res() response: any) {
        try {
            await this.authService.changePassword(request, req.user);
            response.status(201).json();
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }

    @ApiBearerAuth('access-token')
    @UseGuards(CommonGuard)
    @Post('change-language')
    async changeLanguage(@Body() request: any, @Request() req, @Res() response: any) {
        try {
            await this.authService.changeLanguage(request, req.user);
            response.status(201).json();
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }

    // JP-
    @ApiBearerAuth('access-token')
    // @ApiBody({ type: UserLogoutDto })
    @UseGuards(CommonGuard)
    @Post('logout')
    async userLogout(@Request() request, @Res() response: any) {
        try {
            await this.authService.logout(request, request.user);
            response.status(201).json();
        } catch (error) {
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }

    // JP-
    @ApiBody({ type: ForgotPasswordDto })
    @Post('forgot-password')
    async forgotPassword(@Body() request: ForgotPasswordDto, @Res() response: any) {
        try {
            const data = await this.authService.forgotPassword(request);
            response.status(201).json();
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }

    // JP-
    @ApiBearerAuth('access-token')
    @ApiBody({ type: ResetPasswordDto })
    @Post('reset-password')
    async resetPassword(@Body() request: ResetPasswordDto, @Res() response: any) {
        try {
            const data = await this.authService.resetPassword(request);
            response.status(201).json();
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }

    // JP-
    @ApiBearerAuth('access-token')
    @UseGuards(CommonGuard)
    @Get('self')
    async getSelf(@Request() req, @Res() response: any) {
        try {
            const data = await this.authService.self(req.user);
            response.status(200).json(data);
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }

    @Get('user-details/:slug')
    async getUserDetails(@Param() params: UserDetailsQueryDto, @Res() response: any, @Request() req: any) {
        try {
            const data = await this.authService.getUserDetails(params, req);
            response.status(200).json(data);
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }

    @ApiBearerAuth('access-token')
    @UseGuards(CommonGuard)
    @Delete('delete')
    async delete(@Query() query: UserSlugDto, @Request() req, @Res() response: any) {
        try {
            const data = await this.authService.delete(query, req.user);
            response.status(200).json(data);
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }

    @ApiBearerAuth('access-token')
    @UseGuards(AdminGuard)
    @Delete('multi-delete')
    async multiDelete(@Body() body: any, @Request() req, @Res() response: any) {
        try {
            const data = await this.authService.multiDelete(body);
            response.status(200).json(data);
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }

    @UseGuards(CommonGuard)
    @Post('error-log')
    async saveError(@Body() body: any, @Request() req, @Res() response: any) {
        try {
            const data = await this.authService.saveError(body, req.user);
            response.status(200).json(data);
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }

    @Post('verify-whatsapp')
    @UseGuards(CommonGuard)
    async verifyWhatsApp(@Body() body: VerifyWhatsAppDto, @Request() req, @Res() response: any) {
        const data = await this.authService.verifyWhatsApp(body, req.user);
        response.status(200).json(data);
    }
}
