import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../../auth/guards/roles.guard';
import { ModuleGuard } from '../../../common/guards/module.guard';
import { Roles, UserRole } from '../../../common/decorators/roles.decorator';
import { RequireModule } from '../../../common/decorators/require-module.decorator';
import { SubscriberService } from '../services/subscriber.service';

@ApiTags('Telegram Bot — Subscribers')
@ApiBearerAuth()
@Controller('telegram-bot/bots/:botId/subscribers')
@UseGuards(JwtAuthGuard, RolesGuard, ModuleGuard)
@RequireModule('telegram_bot')
@Roles(UserRole.CLUB_ADMIN, UserRole.SUPER_ADMIN)
export class SubscriberController {
  constructor(private readonly subscribers: SubscriberService) {}

  @Get()
  @ApiOperation({ summary: 'List subscribers with filters' })
  async findAll(
    @Param('botId') botId: string,
    @Query('cursor') cursor?: string,
    @Query('limit') limit?: string,
    @Query('search') search?: string,
    @Query('isSubscribed') isSubscribed?: string,
    @Query('bonusGranted') bonusGranted?: string,
    @Query('mailingConsent') mailingConsent?: string,
  ) {
    return this.subscribers.findAll(botId, {
      cursor,
      limit: limit ? parseInt(limit, 10) : undefined,
      search,
      isSubscribed: isSubscribed !== undefined ? isSubscribed === 'true' : undefined,
      bonusGranted: bonusGranted !== undefined ? bonusGranted === 'true' : undefined,
      mailingConsent: mailingConsent !== undefined ? mailingConsent === 'true' : undefined,
    });
  }

  @Get('stats')
  @ApiOperation({ summary: 'Get subscriber statistics' })
  async getStats(@Param('botId') botId: string) {
    return this.subscribers.getStats(botId);
  }
}
