import { Body, Controller, Get, Post, Delete, Patch, Param, Query, UseGuards, HttpCode } from '@nestjs/common';
import { IntegrationsService } from './integrations.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')
@UseGuards(JwtAuthGuard)
export class IntegrationsController {
  constructor(private readonly integrationsService: IntegrationsService) {}

  // --- Apps ---

  @Get('apps')
  async listApps(@Workspace('id') wsId: string) {
    return this.integrationsService.listApps(wsId);
  }

  @Post('apps/install')
  async installApp(
    @Workspace('id') wsId: string,
    @CurrentUser('userId') userId: string,
    @Body() body: { app_slug: string; scopes: string[] },
  ) {
    return this.integrationsService.installApp(wsId, userId, body.app_slug, body.scopes || []);
  }

  @Delete('apps/:id')
  async uninstallApp(@Workspace('id') wsId: string, @Param('id') id: string) {
    return this.integrationsService.uninstallApp(wsId, id);
  }

  // --- Slash Commands ---

  @Get('slash-commands')
  async listSlashCommands(@Workspace('id') wsId: string) {
    return this.integrationsService.listSlashCommands(wsId);
  }

  @Post('slash-commands')
  async createSlashCommand(
    @Workspace('id') wsId: string,
    @CurrentUser('userId') userId: string,
    @Body() body: { command: string; url: string; description?: string; usage_hint?: string },
  ) {
    return this.integrationsService.createSlashCommand(wsId, userId, body);
  }

  @Post('slash-commands/execute')
  async executeSlashCommand(
    @Workspace('id') wsId: string,
    @CurrentUser('userId') userId: string,
    @Body() body: { command: string; text: string; channel_id: string },
  ) {
    return this.integrationsService.executeSlashCommand(wsId, userId, body.command, body.text, body.channel_id);
  }

  // --- Webhooks ---

  @Get('webhooks')
  async listWebhooks(@Workspace('id') wsId: string) {
    return this.integrationsService.listWebhooks(wsId);
  }

  @Post('webhooks')
  async createWebhook(
    @Workspace('id') wsId: string,
    @CurrentUser('userId') userId: string,
    @Body() body: { channel_id: string; name: string },
  ) {
    return this.integrationsService.createWebhook(wsId, userId, body.channel_id, body.name);
  }

  @Post('hooks/:token')
  @HttpCode(200)
  async handleWebhook(@Param('token') token: string, @Body() body: any) {
    return this.integrationsService.handleWebhook(token, body);
  }

  // --- Event Subscriptions ---

  @Get('event-subscriptions')
  async listEventSubscriptions(@Workspace('id') wsId: string) {
    return this.integrationsService.listEventSubscriptions(wsId);
  }

  @Post('event-subscriptions')
  async createEventSubscription(
    @Workspace('id') wsId: string,
    @Body() body: { callback_url: string; event_types: string[] },
  ) {
    return this.integrationsService.createEventSubscription(wsId, {
      callbackUrl: body.callback_url,
      eventTypes: body.event_types || [],
    });
  }

  // --- Workflows ---

  @Get('workflows')
  async listWorkflows(@Workspace('id') wsId: string) {
    return this.integrationsService.listWorkflows(wsId);
  }

  @Post('workflows')
  async createWorkflow(
    @Workspace('id') wsId: string,
    @CurrentUser('userId') userId: string,
    @Body() body: { name: string; description?: string; trigger_type?: string; trigger_config?: any; steps?: any[] },
  ) {
    return this.integrationsService.createWorkflow(wsId, userId, {
      name: body.name,
      description: body.description,
      triggerType: body.trigger_type || 'manual',
      triggerConfig: body.trigger_config || {},
      steps: body.steps || [],
    });
  }

  @Post('workflows/:id/execute')
  async executeWorkflow(@Param('id') id: string, @Body() body: any) {
    return this.integrationsService.executeWorkflow(id, body || {});
  }

  @Patch('workflows/:id')
  async updateWorkflow(
    @Param('id') id: string,
    @Workspace('id') wsId: string,
    @Body() body: { name?: string; description?: string; trigger_type?: string; trigger_config?: any; steps?: any[] },
  ) {
    return this.integrationsService.updateWorkflow(id, wsId, body);
  }

  @Delete('workflows/:id')
  async deleteWorkflow(
    @Param('id') id: string,
    @Workspace('id') wsId: string,
  ) {
    return this.integrationsService.deleteWorkflow(id, wsId);
  }

  @Post('workflows/:id/toggle')
  async toggleWorkflow(
    @Param('id') id: string,
    @Workspace('id') wsId: string,
  ) {
    return this.integrationsService.toggleWorkflow(id, wsId);
  }
}