import { Body, Controller, Get, Param, Post, Query, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiSecurity, ApiTags } from '@nestjs/swagger';
import { Roles } from '../auth/decorators/roles.decorator';
import { RequireScopes } from '../auth/decorators/scopes.decorator';
import { UserOrApiKeyGuard } from '../auth/guards/auth.guard';
import { OrgRolesGuard } from '../auth/guards/org-roles.guard';
import { ScopesGuard } from '../auth/guards/scopes.guard';
import { CreatePointDto, QueryPointsDto } from './dto/points.dto';
import { PointsService } from './points.service';

@ApiTags('points')
@ApiBearerAuth('jwt')
@ApiSecurity('apiKey')
@Controller('orgs/:orgId')
@UseGuards(UserOrApiKeyGuard, OrgRolesGuard, ScopesGuard)
export class PointsController {
  constructor(private readonly pointsService: PointsService) {}

  @Get('jobs/:jobId/points')
  @RequireScopes('points:read')
  listForJob(
    @Param('orgId') orgId: string,
    @Param('jobId') jobId: string,
    @Query() query: QueryPointsDto,
  ) {
    return this.pointsService.listForJob(orgId, jobId, query);
  }

  @Post('jobs/:jobId/points')
  @Roles('MEMBER')
  @RequireScopes('points:write')
  create(
    @Param('orgId') orgId: string,
    @Param('jobId') jobId: string,
    @Body() dto: CreatePointDto,
  ) {
    return this.pointsService.createForJob(orgId, jobId, dto);
  }

  @Get('points')
  @RequireScopes('points:read')
  listForOrg(@Param('orgId') orgId: string, @Query() query: QueryPointsDto) {
    return this.pointsService.listForOrg(orgId, query);
  }
}
