import {
  Controller,
  Get,
  HttpCode,
  HttpStatus,
  UseGuards,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';

import { Action } from '@/casl/action.enum';
import { AppAbility } from '@/casl/casl-ability.factory';
import { CheckPolicies } from '@/casl/check-policies.decorator';
import { PoliciesGuard } from '@/casl/policies.guard';
import { ProgressReportDashboardType } from '@/generated/prisma/client';
import { JwtAuthGuard } from '@/modules/auth/jwt-auth.guard';

import { ProgressReportDashboardsService } from './dashboards.service';

// Every role sees the same published dashboard, so this route is guarded by
// the "Dashboard" permission each of them already has.
const DASHBOARD_SUBJECT = 'Dashboard';

@ApiTags('dashboards')
@ApiBearerAuth('access-token')
@UseGuards(JwtAuthGuard, PoliciesGuard)
@Controller('dashboards')
export class LiveDashboardsController {
  constructor(
    private readonly dashboardsService: ProgressReportDashboardsService,
  ) {}

  // The dashboard CDC-GPSF published. It has no report id in the path because
  // the Ministry, PSWG, CDC and CEFP screens are not tied to one report.
  @Get('pswg/live')
  @HttpCode(HttpStatus.OK)
  @CheckPolicies((ability: AppAbility) =>
    ability.can(Action.Read, DASHBOARD_SUBJECT),
  )
  @ApiOperation({ summary: 'Get the working group dashboard that is live' })
  @ApiResponse({
    status: 200,
    description: 'The published dashboard, or null when there is none yet.',
  })
  findPswgLive() {
    return this.dashboardsService.findLive(ProgressReportDashboardType.PSWG);
  }

  // The plenary dashboard CDC-GPSF published, read by every plenary page.
  @Get('plenary/live')
  @HttpCode(HttpStatus.OK)
  @CheckPolicies((ability: AppAbility) =>
    ability.can(Action.Read, DASHBOARD_SUBJECT),
  )
  @ApiOperation({ summary: 'Get the plenary dashboard that is live' })
  @ApiResponse({
    status: 200,
    description: 'The published dashboard, or null when there is none yet.',
  })
  findPlenaryLive() {
    return this.dashboardsService.findLive(ProgressReportDashboardType.PLENARY);
  }
}
