import { Controller, Post, Body, Req, UseGuards, Patch, Get } from '@nestjs/common';
import { AdminService } from './admin.service';
import { AdminLoginDto, AdminForgotPasswordDto, AdminResetPasswordDto, AdminAcceptInviteDto, AdminChangePasswordDto, UpdateAdminProfileDto } from './dto/admin.dto';
import { AuthGuard } from 'src/auth/auth.guard';
import { RolesGuard } from 'src/guard/role.guard';
import { Roles } from 'src/guard/decorators/role.decorator';
import { Public } from 'src/auth/public.decorator';
import { CommonService } from 'src/common/common.service';
import { PANEL_ROLES } from 'src/user/schema/users.schema';
import { ApiBearerAuth, ApiTags, ApiOperation, ApiBody, ApiResponse } from '@nestjs/swagger';

@ApiTags('Admin')
@Controller('admin')
export class AdminController {
    constructor(
        private readonly adminService: AdminService,
        private readonly common: CommonService
    ) { }

    @Public()
    @Post('auth/login')
    @ApiOperation({ summary: 'Login (super admin, admin, staff) with email and password' })
    @ApiBody({ type: AdminLoginDto })
    @ApiResponse({ status: 201, description: 'Login successful' })
    @ApiResponse({ status: 400, description: 'Invalid credentials' })
    login(@Req() req: any, @Body() dto: AdminLoginDto) {
        return this.adminService.login(dto, this.common.getUserLanguage(req));
    }

    @Public()
    @Post('auth/forgot-password')
    @ApiOperation({ summary: 'Email a password reset link' })
    @ApiBody({ type: AdminForgotPasswordDto })
    @ApiResponse({ status: 201, description: 'Reset link sent if the account exists' })
    forgotPassword(@Req() req: any, @Body() dto: AdminForgotPasswordDto) {
        return this.adminService.forgotPassword(dto, this.common.getUserLanguage(req));
    }

    @Public()
    @Post('auth/reset-password')
    @ApiOperation({ summary: 'Set a new password using the token from the reset link' })
    @ApiBody({ type: AdminResetPasswordDto })
    @ApiResponse({ status: 201, description: 'Password reset successfully' })
    @ApiResponse({ status: 400, description: 'Invalid or expired token' })
    resetPassword(@Req() req: any, @Body() dto: AdminResetPasswordDto) {
        return this.adminService.resetPassword(dto, this.common.getUserLanguage(req));
    }

    @Public()
    @Post('auth/accept-invite')
    @ApiOperation({ summary: 'Set the first password using the token from the staff invite link' })
    @ApiBody({ type: AdminAcceptInviteDto })
    @ApiResponse({ status: 201, description: 'Invite accepted, you can now log in' })
    @ApiResponse({ status: 400, description: 'Invalid or expired invite' })
    acceptInvite(@Req() req: any, @Body() dto: AdminAcceptInviteDto) {
        return this.adminService.acceptInvite(dto, this.common.getUserLanguage(req));
    }

    @UseGuards(AuthGuard, RolesGuard)
    @Roles(...PANEL_ROLES)
    @ApiBearerAuth('access_token')
    @Patch('auth/change-password')
    @ApiOperation({ summary: 'Change password of the logged in user' })
    @ApiBody({ type: AdminChangePasswordDto })
    @ApiResponse({ status: 200, description: 'Password changed successfully' })
    changePassword(@Req() req: any, @Body() dto: AdminChangePasswordDto) {
        return this.adminService.changePassword(req.user_data._id, dto, this.common.getUserLanguage(req));
    }

    @UseGuards(AuthGuard, RolesGuard)
    @Roles(...PANEL_ROLES)
    @ApiBearerAuth('access_token')
    @Post('auth/logout')
    @ApiOperation({ summary: 'Logout and clear sessions' })
    @ApiResponse({ status: 201, description: 'Logged out successfully' })
    logout(@Req() req: any) {
        return this.adminService.logout(req.user_data._id, this.common.getUserLanguage(req));
    }

    @UseGuards(AuthGuard, RolesGuard)
    @Roles(...PANEL_ROLES)
    @ApiBearerAuth('access_token')
    @Get('profile')
    @ApiOperation({ summary: 'Get profile of the logged in user' })
    @ApiResponse({ status: 200, description: 'Profile retrieved successfully' })
    getProfile(@Req() req: any) {
        return this.adminService.getProfile(req.user_data._id, this.common.getUserLanguage(req));
    }

    @UseGuards(AuthGuard, RolesGuard)
    @Roles(...PANEL_ROLES)
    @ApiBearerAuth('access_token')
    @Patch('profile')
    @ApiOperation({ summary: 'Update profile of the logged in user' })
    @ApiBody({ type: UpdateAdminProfileDto })
    @ApiResponse({ status: 200, description: 'Profile updated successfully' })
    updateProfile(@Req() req: any, @Body() dto: UpdateAdminProfileDto) {
        return this.adminService.updateProfile(req.user_data._id, dto, this.common.getUserLanguage(req));
    }
}
