import { Body, Controller, Get, Post, Delete, Patch, Param, UseGuards } from '@nestjs/common';
import { OutgoingWebhooksService } from './outgoing-webhooks.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/outgoing-webhooks')
@UseGuards(JwtAuthGuard)
export class OutgoingWebhooksController {
  constructor(private readonly outgoingWebhooksService: OutgoingWebhooksService) {}

  /**
   * GET /integrations/outgoing-webhooks
   * Lista los outgoing webhooks del workspace.
   */
  @Get()
  async list(@Workspace('id') wsId: string) {
    return this.outgoingWebhooksService.listOutgoingWebhooks(wsId);
  }

  /**
   * GET /integrations/outgoing-webhooks/:id
   * Obtiene un outgoing webhook específico.
   */
  @Get(':id')
  async getOne(@Workspace('id') wsId: string, @Param('id') id: string) {
    return this.outgoingWebhooksService.getOutgoingWebhook(wsId, id);
  }

  /**
   * POST /integrations/outgoing-webhooks
   * Crea un nuevo outgoing webhook.
   */
  @Post()
  async create(
    @Workspace('id') wsId: string,
    @CurrentUser('userId') _userId: string,
    @Body() body: { callback_url: string; event_types: string[]; app_id?: string },
  ) {
    return this.outgoingWebhooksService.createOutgoingWebhook(wsId, body);
  }

  /**
   * PATCH /integrations/outgoing-webhooks/:id
   * Actualiza un outgoing webhook.
   */
  @Patch(':id')
  async update(
    @Workspace('id') wsId: string,
    @Param('id') id: string,
    @Body() body: { callback_url?: string; event_types?: string[] },
  ) {
    return this.outgoingWebhooksService.updateOutgoingWebhook(wsId, id, body);
  }

  /**
   * POST /integrations/outgoing-webhooks/:id/activate
   * Activa un outgoing webhook.
   */
  @Post(':id/activate')
  async activate(@Workspace('id') wsId: string, @Param('id') id: string) {
    return this.outgoingWebhooksService.activateOutgoingWebhook(wsId, id);
  }

  /**
   * POST /integrations/outgoing-webhooks/:id/deactivate
   * Desactiva un outgoing webhook.
   */
  @Post(':id/deactivate')
  async deactivate(@Workspace('id') wsId: string, @Param('id') id: string) {
    return this.outgoingWebhooksService.deactivateOutgoingWebhook(wsId, id);
  }

  /**
   * DELETE /integrations/outgoing-webhooks/:id
   * Elimina un outgoing webhook.
   */
  @Delete(':id')
  async delete(@Workspace('id') wsId: string, @Param('id') id: string) {
    return this.outgoingWebhooksService.deleteOutgoingWebhook(wsId, id);
  }

  /**
   * POST /integrations/outgoing-webhooks/:id/test
   * Envía un evento de prueba al webhook.
   */
  @Post(':id/test')
  async test(@Workspace('id') wsId: string, @Param('id') id: string) {
    return this.outgoingWebhooksService.testOutgoingWebhook(wsId, id);
  }

  /**
   * POST /integrations/outgoing-webhooks/dispatch
   * Dispara un evento a todos los webhooks suscritos.
   * Body: { event_type: string, event_data: any }
   */
  @Post('dispatch')
  async dispatch(
    @Workspace('id') wsId: string,
    @Body() body: { event_type: string; event_data: any },
  ) {
    return this.outgoingWebhooksService.dispatchEvent(wsId, body.event_type, body.event_data);
  }
}