import {
  Controller,
  Get,
  Param,
  Query,
  Res,
  ParseIntPipe,
  UseGuards,
} from '@nestjs/common';
import { Response } from 'express';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiSecurity, ApiParam, ApiQuery } from '@nestjs/swagger';
import { ResponseService } from 'src/common/response-handling/response-handler';
import { handleControllerError } from 'src/common/utils/controller-response-handling';
import { RolesGuard } from 'src/common/guards/roles.guard';
import { Roles } from 'src/common/decorators/roles.decorator';
import { CombinedAuthGuard } from 'src/auth/combined-auth.guard';
import { ZoomAnalyticsFacadeService } from 'src/zoom/services/zoom-analytics-facade.service';
import { ANALYTICS_SWAGGER, ANALYTICS_RESPONSE } from '../constants/analytics.constants';

/**
 * Seeker-scoped analytics (cross-session rollup for the seeker-detail screen).
 * Same shape as AnalyticsController: zero DB access here — everything happens
 * inside the Zoom module via ZoomAnalyticsFacadeService.
 */
@ApiTags(ANALYTICS_SWAGGER.TAG)
@Controller('analytics/seekers')
@UseGuards(CombinedAuthGuard, RolesGuard)
@Roles('admin', 'shoba', 'relational_manager')
@ApiBearerAuth('Authorization')
@ApiSecurity('userIdAuth')
@ApiSecurity('activeRoleAuth')
export class AnalyticsSeekerController {
  constructor(
    private readonly facade: ZoomAnalyticsFacadeService,
    private readonly responseService: ResponseService,
  ) {}

  @Get(':registrationId')
  @ApiOperation({ summary: ANALYTICS_SWAGGER.GET_SEEKER_ANALYTICS })
  @ApiParam({ name: 'registrationId', type: Number })
  @ApiQuery({ name: 'programId', type: Number })
  async getSeekerAnalytics(
    @Param('registrationId', ParseIntPipe) registrationId: number,
    @Query('programId', ParseIntPipe) programId: number,
    @Res() res: Response,
  ) {
    try {
      const data = await this.facade.getSeekerAnalytics(programId, registrationId);
      await this.responseService.success(res, ANALYTICS_RESPONSE.SEEKER_ANALYTICS_FETCHED, data);
    } catch (error) {
      handleControllerError(res, error);
    }
  }
}
