import { Body, Controller, Get, Post, Req, UseGuards } from '@nestjs/common';
import { UserRole } from '@prisma/client';
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
import { AuthService } from './auth.service';
import { LoginDto } from './dto/login.dto';
import { RefreshTokenDto } from './dto/refresh-token.dto';
import { RegisterDriverDto } from './dto/register-driver.dto';

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

  @Post('login')
  login(@Body() body: LoginDto) {
    return this.authService.login(body, body.role);
  }

  @Post('login/customer')
  loginCustomer(@Body() body: LoginDto) {
    return this.authService.login(body, UserRole.CUSTOMER);
  }

  @Post('login/driver')
  loginDriver(@Body() body: LoginDto) {
    return this.authService.login(body, UserRole.DRIVER);
  }

  @Post('register/driver')
  registerDriver(@Body() body: RegisterDriverDto) {
    return this.authService.registerDriver(body);
  }

  @Post('login/restaurant')
  loginRestaurant(@Body() body: LoginDto) {
    return this.authService.login(body, UserRole.RESTAURANT);
  }

  @Post('login/admin')
  loginAdmin(@Body() body: LoginDto) {
    return this.authService.login(body, [UserRole.ADMIN, UserRole.SUPER_ADMIN]);
  }

  @Post('refresh')
  refresh(@Body() body: RefreshTokenDto) {
    return this.authService.refresh(body.refreshToken);
  }

  @Get('me')
  @UseGuards(JwtAuthGuard)
  me(@Req() req: { user: { sub: string } }) {
    return this.authService.me(req.user.sub);
  }
}
