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 { ScenarioService } from '../services/scenario.service';
import { CreateScenarioDto, UpdateScenarioDto } from '../dto/create-scenario.dto';
import { BotScenario } from '../interfaces/scenario.interface';

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

  @Post()
  @ApiOperation({ summary: 'Create a new scenario' })
  async create(@CurrentUser() user: { clubId: string }, @Body() dto: CreateScenarioDto) {
    return this.scenarios.create(user.clubId, dto.name, dto.description);
  }

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

  @Get(':id')
  @ApiOperation({ summary: 'Get scenario with full JSON' })
  async findOne(@CurrentUser() user: { clubId: string }, @Param('id') id: string) {
    return this.scenarios.findOne(user.clubId, id);
  }

  @Patch(':id')
  @ApiOperation({ summary: 'Update scenario (name, description, or JSON)' })
  async update(
    @CurrentUser() user: { clubId: string },
    @Param('id') id: string,
    @Body() dto: UpdateScenarioDto,
  ) {
    return this.scenarios.update(user.clubId, id, dto);
  }

  @Post(':id/publish')
  @ApiOperation({ summary: 'Publish scenario (validates and marks as published)' })
  async publish(@CurrentUser() user: { clubId: string }, @Param('id') id: string) {
    return this.scenarios.publish(user.clubId, id);
  }

  @Post(':id/duplicate')
  @ApiOperation({ summary: 'Duplicate scenario' })
  async duplicate(@CurrentUser() user: { clubId: string }, @Param('id') id: string) {
    return this.scenarios.duplicate(user.clubId, id);
  }

  @Delete(':id')
  @ApiOperation({ summary: 'Delete scenario (fails if used by a bot)' })
  async remove(@CurrentUser() user: { clubId: string }, @Param('id') id: string) {
    return this.scenarios.remove(user.clubId, id);
  }

  @Post(':id/validate')
  @ApiOperation({ summary: 'Validate scenario JSON without saving' })
  async validate(@CurrentUser() user: { clubId: string }, @Param('id') id: string) {
    const scenario = await this.scenarios.findOne(user.clubId, id);
    this.scenarios.validateScenario(scenario.scenario as unknown as BotScenario);
    return { valid: true };
  }
}
