import { Controller, Get, Post, Body, Req, Param } from '@nestjs/common';
import { Request } from 'express';
import { ApiTags, ApiResponse, ApiOperation } from '@nestjs/swagger';
import { DriverService } from './DriverService';
import { DriversLeaderboardDTO } from './dtos/DriversLeaderboardDTO';
import { DriverStatsDTO } from './dtos/DriverStatsDTO';
import { CompleteOnboardingInputDTO } from './dtos/CompleteOnboardingInputDTO';
import { CompleteOnboardingOutputDTO } from './dtos/CompleteOnboardingOutputDTO';
import { GetDriverRegistrationStatusQueryDTO } from './dtos/GetDriverRegistrationStatusQueryDTO';
import { DriverRegistrationStatusDTO } from './dtos/DriverRegistrationStatusDTO';
import { DriverDTO } from './dtos/DriverDTO';

@ApiTags('drivers')
@Controller('drivers')
export class DriverController {
  constructor(private readonly driverService: DriverService) {}

  @Get('leaderboard')
  @ApiOperation({ summary: 'Get drivers leaderboard' })
  @ApiResponse({ status: 200, description: 'List of drivers for the leaderboard', type: DriversLeaderboardDTO })
  async getDriversLeaderboard(): Promise<DriversLeaderboardDTO> {
    return this.driverService.getDriversLeaderboard();
  }

  @Get('total-drivers')
  @ApiOperation({ summary: 'Get the total number of drivers' })
  @ApiResponse({ status: 200, description: 'Total number of drivers', type: DriverStatsDTO })
  async getTotalDrivers(): Promise<DriverStatsDTO> {
    return this.driverService.getTotalDrivers();
  }

  @Get('current')
  @ApiOperation({ summary: 'Get current authenticated driver' })
  @ApiResponse({ status: 200, description: 'Current driver data', type: DriverDTO })
  @ApiResponse({ status: 404, description: 'Driver not found' })
  async getCurrentDriver(@Req() req: Request): Promise<DriverDTO | null> {
    // Assuming userId is available from the request (e.g., via auth middleware)
    const userId = req['user']?.userId;
    if (!userId) {
      return null;
    }
    return this.driverService.getCurrentDriver(userId);
  }

  @Post('complete-onboarding')
  @ApiOperation({ summary: 'Complete driver onboarding for a user' })
  @ApiResponse({ status: 200, description: 'Onboarding complete', type: CompleteOnboardingOutputDTO })
  async completeOnboarding(
    @Body() input: CompleteOnboardingInputDTO,
    @Req() req: Request,
  ): Promise<CompleteOnboardingOutputDTO> {
    // Assuming userId is available from the request (e.g., via auth middleware)
    const userId = req['user'].userId; // Placeholder for actual user extraction
    return this.driverService.completeOnboarding(userId, input);
  }

  @Get(':driverId/races/:raceId/registration-status')
  @ApiOperation({ summary: 'Get driver registration status for a specific race' })
  @ApiResponse({ status: 200, description: 'Driver registration status', type: DriverRegistrationStatusDTO })
  async getDriverRegistrationStatus(
    @Param('driverId') driverId: string,
    @Param('raceId') raceId: string,
  ): Promise<DriverRegistrationStatusDTO> {
    return this.driverService.getDriverRegistrationStatus({ driverId, raceId });
  }

  @Put(':driverId/profile')
  @ApiOperation({ summary: 'Update driver profile' })
  @ApiResponse({ status: 200, description: 'Driver profile updated', type: DriverDTO })
  async updateDriverProfile(
    @Param('driverId') driverId: string,
    @Body() body: { bio?: string; country?: string },
  ): Promise<DriverDTO | null> {
    return this.driverService.updateDriverProfile(driverId, body.bio, body.country);
  }

  // Add other Driver endpoints here based on other presenters
}
