import {
  Controller,
  Get,
  Query,
  Res,
  HttpStatus,
  UseGuards,
} from '@nestjs/common';
import { Response } from 'express';
import {
  ApiTags,
  ApiOperation,
  ApiBearerAuth,
  ApiSecurity,
  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 { WebinarService } from '../sessions/webinar.service';
import { ZOOM_SWAGGER } from 'src/common/constants/zoom.constants';

/**
 * Endpoint (admin/RM/Coordinator) to list existing Zoom webinar templates (informational).
 * Templates are neither created nor applied to programs — webinars/meetings are
 * provisioned with the required settings (notably "no communication") applied
 * directly on create.
 */
@ApiTags(ZOOM_SWAGGER.TAG)
@Controller('zoom/webinar-templates')
@UseGuards(CombinedAuthGuard, RolesGuard)
@Roles('admin', 'shoba', 'relational_manager')
@ApiBearerAuth('Authorization')
@ApiSecurity('userIdAuth')
@ApiSecurity('activeRoleAuth')
export class ZoomTemplateController {
  constructor(
    private readonly webinarService: WebinarService,
    private readonly responseService: ResponseService,
  ) {}

  @Get()
  @ApiOperation({ summary: ZOOM_SWAGGER.LIST_TEMPLATES })
  @ApiQuery({
    name: 'hostEmail',
    required: false,
    description: 'Host account to list templates for. Defaults to ZOOM_ADMIN_EMAIL.',
  })
  async list(@Query('hostEmail') hostEmail: string | undefined, @Res() res: Response) {
    try {
      const templates = await this.webinarService.listTemplates(hostEmail);
      await this.responseService.success(res, 'Zoom templates', templates, HttpStatus.OK);
    } catch (error) {
      handleControllerError(res, error);
    }
  }
}
