import { Body, Controller, Get, Post, Delete, Patch, Param, Query, UseGuards } from '@nestjs/common';
import { BotUsersService } from './bot-users.service';
import { JwtAuthGuard } from '../../common/guards/jwt.guard';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { Workspace } from '../../common/decorators/workspace.decorator';

@Controller('integrations/bots')
@UseGuards(JwtAuthGuard)
export class BotUsersController {
  constructor(private readonly botUsersService: BotUsersService) {}

  /**
   * GET /integrations/bots
   * Lista los bot users del workspace.
   * Query params: app_id (filtro), active (true/false)
   */
  @Get()
  async listBots(
    @Workspace('id') wsId: string,
    @Query('app_id') appId?: string,
    @Query('active') active?: string,
  ) {
    return this.botUsersService.listBots(wsId, {
      appId,
      isActive: active === 'true' ? true : active === 'false' ? false : undefined,
    });
  }

  /**
   * GET /integrations/bots/:id
   * Obtiene un bot user por ID.
   */
  @Get(':id')
  async getBot(@Workspace('id') wsId: string, @Param('id') id: string) {
    return this.botUsersService.getBot(wsId, id);
  }

  /**
   * POST /integrations/bots
   * Crea un nuevo bot user.
   */
  @Post()
  async createBot(
    @Workspace('id') wsId: string,
    @CurrentUser('userId') _userId: string,
    @Body() body: { name: string; display_name: string; app_id?: string; avatar_url?: string },
  ) {
    return this.botUsersService.createBot(wsId, body);
  }

  /**
   * PATCH /integrations/bots/:id
   * Actualiza un bot user.
   */
  @Patch(':id')
  async updateBot(
    @Workspace('id') wsId: string,
    @Param('id') id: string,
    @Body() body: { display_name?: string; avatar_url?: string; name?: string },
  ) {
    return this.botUsersService.updateBot(wsId, id, body);
  }

  /**
   * POST /integrations/bots/:id/activate
   * Activa un bot user.
   */
  @Post(':id/activate')
  async activateBot(@Workspace('id') wsId: string, @Param('id') id: string) {
    return this.botUsersService.activateBot(wsId, id);
  }

  /**
   * POST /integrations/bots/:id/deactivate
   * Desactiva un bot user.
   */
  @Post(':id/deactivate')
  async deactivateBot(@Workspace('id') wsId: string, @Param('id') id: string) {
    return this.botUsersService.deactivateBot(wsId, id);
  }

  /**
   * DELETE /integrations/bots/:id
   * Elimina un bot user.
   */
  @Delete(':id')
  async deleteBot(@Workspace('id') wsId: string, @Param('id') id: string) {
    return this.botUsersService.deleteBot(wsId, id);
  }
}