import { Controller, Get, Inject, Query, Req, UnauthorizedException } from '@nestjs/common';
import { ApiOperation, ApiQuery, ApiResponse, ApiTags } from '@nestjs/swagger';
import { DashboardService } from './DashboardService';
import { DashboardOverviewDTO } from './dtos/DashboardOverviewDTO';

type AuthenticatedRequest = {
  user?: { userId: string; primaryDriverId?: string };
};

@ApiTags('dashboard')
@Controller('dashboard')
export class DashboardController {
  constructor(@Inject(DashboardService) private readonly dashboardService: DashboardService) {}

  @Get('overview')
  @ApiOperation({ summary: 'Get dashboard overview' })
  @ApiQuery({ name: 'driverId', description: 'Driver ID' })
  @ApiResponse({ status: 200, description: 'Dashboard overview', type: DashboardOverviewDTO })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  async getDashboardOverview(
    @Query('driverId') _driverId: string,
    @Req() req: AuthenticatedRequest,
  ): Promise<DashboardOverviewDTO> {
    const driverId = req.user?.primaryDriverId;
    if (!driverId) {
      throw new UnauthorizedException('Unauthorized: No driver associated with user');
    }
    return this.dashboardService.getDashboardOverview(driverId);
  }
}