import { Controller, Post, Body, UseGuards } from '@nestjs/common';
import { AuthService } from '../services/auth.service';
import { RequestOtpDto } from '../dto/request-otp.dto';
import { DirectLoginDto } from '../dto/direct-login.dto';
import { ApiTags, ApiOperation, ApiBody } from '@nestjs/swagger';
import { VerifyOtpDto } from '../dto/verify-otp.dto';
import { Throttle } from '@nestjs/throttler';
import { RefreshTokenRateLimit } from 'src/common/decorators/rate-limit.decorator';
import { RefreshTokenDto } from '../dto/refresh-token.dto';
import { SuperAdminAuthGuard } from '../guards/superAdminAuth.guard';

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

  @Throttle({ default: { limit: 3, ttl: 180_000 } })
  @Post('public/auth/otp/request')
  @ApiOperation({ summary: 'Request OTP for login or signup' })
  @ApiBody({ type: RequestOtpDto, description: 'Mobile number to receive OTP' })
  otpRequest(@Body() dto: RequestOtpDto) {
    return this.authService.requestOtp(dto, false);
  }

  @Post('public/auth/otp/verify')
  @ApiOperation({ summary: 'Verify OTP code' })
  @ApiBody({ type: VerifyOtpDto, description: 'Mobile number and OTP code' })
  otpVerify(@Body() dto: VerifyOtpDto) {
    return this.authService.verifyOtp(dto.phone, dto.slug, dto.otp); // assuming dto has `otp` property
  }

  @RefreshTokenRateLimit()
  @ApiOperation({ summary: 'refresh the user access token / refresh token' })
  @Post('public/auth/refresh')
  refreshToken(@Body() refreshTokenDto: RefreshTokenDto) {
    return this.authService.refreshToken(refreshTokenDto.refreshToken);
  }

  @Throttle({ default: { limit: 3, ttl: 180_000 } })
  @Post('admin/auth/otp/request')
  @ApiOperation({ summary: 'Request OTP for login or signup' })
  @ApiBody({ type: RequestOtpDto, description: 'Mobile number to receive OTP' })
  otpRequestAdmin(@Body() dto: RequestOtpDto) {
    return this.authService.requestOtp(dto, true);
  }

  @Post('admin/auth/otp/verify')
  @ApiOperation({ summary: 'Verify OTP code' })
  @ApiBody({ type: VerifyOtpDto, description: 'Mobile number and OTP code' })
  otpVerifyAdmin(@Body() dto: VerifyOtpDto) {
    return this.authService.verifyOtpAdmin(dto.phone, dto.slug, dto.otp);
  }

  @RefreshTokenRateLimit()
  @ApiOperation({ summary: 'refresh the admin access token / refresh token' })
  @Post('admin/auth/refresh')
  refreshTokenAdmin(@Body() refreshTokenDto: RefreshTokenDto) {
    return this.authService.refreshToken(refreshTokenDto.refreshToken);
  }

  //super admin routes

  @UseGuards(SuperAdminAuthGuard)
  @Post('super-admin/auth/direct-login')
  @ApiOperation({ summary: 'Direct login for DSC - returns login credentials' })
  @ApiBody({ type: DirectLoginDto, description: 'Phone number and restaurant slug for direct login' })
  directloginForDsc(@Body() dto: DirectLoginDto) {
    return this.authService.loginAdminForDsc(dto.phone, dto.slug);
  }

}
