import { Body, Controller, Post, UseGuards, HttpCode } from '@nestjs/common';
import { InteractiveComponentsService } from './interactive-components.service';
import { JwtAuthGuard } from '../../common/guards/jwt.guard';
import { Workspace } from '../../common/decorators/workspace.decorator';

@Controller('integrations/interactive')
@UseGuards(JwtAuthGuard)
export class InteractiveComponentsController {
  constructor(private readonly interactiveService: InteractiveComponentsService) {}

  /**
   * POST /integrations/interactive
   * Endpoint unificado para recibir payloads interactivos.
   * El cuerpo debe incluir un campo `type` que determina el tipo de interacción:
   *   - block_actions: clicks en botones, selección de menús
   *   - view_submission: envío de modales/formularios
   *   - view_closed: cierre de modales
   *   - shortcut: invocaciones de shortcuts
   */
  @Post()
  @HttpCode(200)
  async handlePayload(@Workspace('id') wsId: string, @Body() body: any) {
    return this.interactiveService.handleInteractivePayload(wsId, body);
  }

  /**
   * POST /integrations/interactive/block-actions
   * Handler específico para block_actions.
   */
  @Post('block-actions')
  @HttpCode(200)
  async handleBlockActions(@Workspace('id') wsId: string, @Body() body: any) {
    return this.interactiveService.handleBlockActions(wsId, body);
  }

  /**
   * POST /integrations/interactive/view-submission
   * Handler específico para view_submission.
   */
  @Post('view-submission')
  @HttpCode(200)
  async handleViewSubmission(@Workspace('id') wsId: string, @Body() body: any) {
    return this.interactiveService.handleViewSubmission(wsId, body);
  }

  /**
   * POST /integrations/interactive/view-closed
   * Handler específico para view_closed.
   */
  @Post('view-closed')
  @HttpCode(200)
  async handleViewClosed(@Workspace('id') wsId: string, @Body() body: any) {
    return this.interactiveService.handleViewClosed(wsId, body);
  }

  /**
   * POST /integrations/interactive/shortcuts
   * Handler específico para shortcuts.
   */
  @Post('shortcuts')
  @HttpCode(200)
  async handleShortcut(@Workspace('id') wsId: string, @Body() body: any) {
    return this.interactiveService.handleShortcut(wsId, body);
  }

  /**
   * POST /integrations/interactive/open-modal
   * Abre un modal interactivo usando trigger_id.
   */
  @Post('open-modal')
  @HttpCode(200)
  async openModal(
    @Workspace('id') wsId: string,
    @Body() body: { trigger_id: string; view: any },
  ) {
    return this.interactiveService.openModal(wsId, body.trigger_id, body.view);
  }

  /**
   * POST /integrations/interactive/update-modal
   * Actualiza un modal abierto.
   */
  @Post('update-modal')
  @HttpCode(200)
  async updateModal(
    @Workspace('id') wsId: string,
    @Body() body: { external_id: string; view: any },
  ) {
    return this.interactiveService.updateModal(wsId, body.external_id, body.view);
  }

  /**
   * POST /integrations/interactive/post-ephemeral
   * Publica un mensaje efímero a un usuario en un canal.
   */
  @Post('post-ephemeral')
  @HttpCode(200)
  async postEphemeral(
    @Workspace('id') wsId: string,
    @Body() body: { channel_id: string; user_id: string; text?: string; blocks?: any[] },
  ) {
    return this.interactiveService.postEphemeral(wsId, body);
  }
}