import { Body, Controller, ForbiddenException, HttpCode, HttpStatus, Inject, InternalServerErrorException, NotFoundException, Param, Post, Req, UnauthorizedException } from '@nestjs/common';
import { ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger';
import { ProtestsService } from './ProtestsService';
import { ReviewProtestCommandDTO } from '../race/dtos/ReviewProtestCommandDTO';
import type { ReviewProtestResponseDTO } from './presenters/ReviewProtestPresenter';

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

@ApiTags('protests')
@Controller('protests')
export class ProtestsController {
  constructor(@Inject(ProtestsService) private readonly protestsService: ProtestsService) {}

  @Post(':protestId/review')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: 'Review a protest' })
  @ApiParam({ name: 'protestId', description: 'Protest ID' })
  @ApiResponse({ status: 200, description: 'Protest reviewed successfully' })
  async reviewProtest(
    @Param('protestId') protestId: string,
    @Body() body: Omit<ReviewProtestCommandDTO, 'protestId' | 'stewardId'>,
    @Req() req: AuthenticatedRequest,
  ): Promise<void> {
    const userId = req.user?.userId;
    if (!userId) {
      throw new UnauthorizedException('Unauthorized');
    }

    const result: ReviewProtestResponseDTO = await this.protestsService.reviewProtest({
      protestId,
      stewardId: userId,
      ...body,
    });

    if (!result.success) {
      switch (result.errorCode) {
        case 'PROTEST_NOT_FOUND':
          throw new NotFoundException(result.message ?? 'Protest not found');
        case 'RACE_NOT_FOUND':
          throw new NotFoundException(result.message ?? 'Race not found for protest');
        case 'NOT_LEAGUE_ADMIN':
          throw new ForbiddenException(result.message ?? 'Steward is not authorized to review this protest');
        default:
          throw new InternalServerErrorException(result.message ?? 'Failed to review protest');
      }
    }
  }
}
