import { Controller, Get, Post, Patch, Delete, 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 { BotManagerService } from '../services/bot-manager.service';
import { CreateBotDto } from '../dto/create-bot.dto';
import { UpdateBotDto } from '../dto/update-bot.dto';

@ApiTags('Telegram Bot')
@ApiBearerAuth()
@Controller('telegram-bot/bots')
@UseGuards(JwtAuthGuard, RolesGuard, ModuleGuard)
@RequireModule('telegram_bot')
@Roles(UserRole.CLUB_ADMIN, UserRole.SUPER_ADMIN)
export class BotController {
  constructor(private readonly botManager: BotManagerService) {}

  @Post()
  @ApiOperation({ summary: 'Create a new Telegram bot' })
  async create(@CurrentUser() user: { clubId: string }, @Body() dto: CreateBotDto) {
    return this.botManager.create(user.clubId, dto);
  }

  @Get()
  @ApiOperation({ summary: 'List all bots for the club' })
  async findAll(@CurrentUser() user: { clubId: string }) {
    return this.botManager.findAll(user.clubId);
  }

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

  @Patch(':id')
  @ApiOperation({ summary: 'Update bot settings' })
  async update(
    @CurrentUser() user: { clubId: string },
    @Param('id') id: string,
    @Body() dto: UpdateBotDto,
  ) {
    return this.botManager.update(user.clubId, id, dto);
  }

  @Delete(':id')
  @ApiOperation({ summary: 'Delete a bot' })
  async remove(@CurrentUser() user: { clubId: string }, @Param('id') id: string) {
    return this.botManager.remove(user.clubId, id);
  }

  @Post(':id/activate')
  @ApiOperation({ summary: 'Activate bot (set Telegram webhook)' })
  async activate(@CurrentUser() user: { clubId: string }, @Param('id') id: string) {
    return this.botManager.activate(user.clubId, id);
  }

  @Post(':id/deactivate')
  @ApiOperation({ summary: 'Deactivate bot (remove Telegram webhook)' })
  async deactivate(@CurrentUser() user: { clubId: string }, @Param('id') id: string) {
    return this.botManager.deactivate(user.clubId, id);
  }
}
