import { Controller, Get, Post, Patch, Body, Param, 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 { CurrentUser } from '../../../common/decorators/current-user.decorator';
import { RequireModule } from '../../../common/decorators/require-module.decorator';
import {
  BroadcastService,
  BroadcastMessage,
  BroadcastFilters,
} from '../services/broadcast.service';
import { CreateBroadcastDto, UpdateBroadcastDto, PreviewCountDto } from '../dto/broadcast.dto';

@ApiTags('Telegram Bot — Broadcasts')
@ApiBearerAuth()
@Controller('telegram-bot/broadcasts')
@UseGuards(JwtAuthGuard, RolesGuard, ModuleGuard)
@RequireModule('telegram_bot')
@Roles(UserRole.CLUB_ADMIN, UserRole.SUPER_ADMIN)
export class BroadcastController {
  constructor(private readonly broadcasts: BroadcastService) {}

  @Post()
  @ApiOperation({ summary: 'Create broadcast' })
  async create(@CurrentUser() user: { clubId: string }, @Body() dto: CreateBroadcastDto) {
    return this.broadcasts.create(user.clubId, dto.botId, {
      name: dto.name,
      message: dto.message as unknown as BroadcastMessage,
      filters: dto.filters as unknown as BroadcastFilters,
      scheduledAt: dto.scheduledAt,
    });
  }

  @Get()
  @ApiOperation({ summary: 'List broadcasts' })
  async findAll(@CurrentUser() user: { clubId: string }) {
    return this.broadcasts.findAll(user.clubId);
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get broadcast details' })
  async findOne(@CurrentUser() user: { clubId: string }, @Param('id') id: string) {
    return this.broadcasts.findOne(user.clubId, id);
  }

  @Patch(':id')
  @ApiOperation({ summary: 'Update broadcast (DRAFT only)' })
  async update(
    @CurrentUser() user: { clubId: string },
    @Param('id') id: string,
    @Body() dto: UpdateBroadcastDto,
  ) {
    return this.broadcasts.update(user.clubId, id, {
      name: dto.name,
      message: dto.message as BroadcastMessage | undefined,
      filters: dto.filters as BroadcastFilters | undefined,
      scheduledAt: dto.scheduledAt,
    });
  }

  @Post(':id/send')
  @ApiOperation({ summary: 'Launch broadcast' })
  async send(@CurrentUser() user: { clubId: string }, @Param('id') id: string) {
    return this.broadcasts.send(user.clubId, id);
  }

  @Post(':id/cancel')
  @ApiOperation({ summary: 'Cancel sending broadcast' })
  async cancel(@CurrentUser() user: { clubId: string }, @Param('id') id: string) {
    return this.broadcasts.cancel(user.clubId, id);
  }

  @Post('preview-count')
  @ApiOperation({ summary: 'Preview audience count without sending' })
  async previewCount(@Body() dto: PreviewCountDto) {
    return this.broadcasts.previewCount(dto.botId, dto.filters as BroadcastFilters);
  }
}
