import { Body, Controller, Post, Get, Delete, Param, Query, UseGuards } from '@nestjs/common';
import { AiService } from './ai.service';
import { AiSearchService } from './ai-search.service';
import { EmbeddingsService } from './embeddings.service';
import { WorkflowGenerationService } from './workflow-generation.service';
import { HuddleNotesService } from './huddle-notes.service';
import { JwtAuthGuard } from '../../common/guards/jwt.guard';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { Workspace } from '../../common/decorators/workspace.decorator';

@Controller('ai')
@UseGuards(JwtAuthGuard)
export class AiController {
  constructor(
    private readonly aiService: AiService,
    private readonly aiSearchService: AiSearchService,
    private readonly embeddingsService: EmbeddingsService,
    private readonly workflowGenerationService: WorkflowGenerationService,
    private readonly huddleNotesService: HuddleNotesService,
  ) {}

  // ============ Endpoints existentes (FASE 5 inicial) ============

  @Post('summarize/channel/:channelId')
  async summarizeChannel(
    @Param('channelId') channelId: string,
    @Workspace('id') wsId: string,
    @CurrentUser('userId') userId: string,
  ) {
    return this.aiService.summarizeChannel(channelId, wsId, userId);
  }

  @Post('summarize/thread/:messageId')
  async summarizeThread(
    @Param('messageId') messageId: string,
    @Workspace('id') wsId: string,
    @CurrentUser('userId') userId: string,
  ) {
    return this.aiService.summarizeThread(messageId, wsId, userId);
  }

  @Post('ask')
  async ask(
    @Workspace('id') wsId: string,
    @CurrentUser('userId') userId: string,
    @Body() dto: any,
  ) {
    return this.aiService.ask(wsId, userId, dto);
  }

  @Post('daily-recap')
  async dailyRecap(
    @Workspace('id') wsId: string,
    @CurrentUser('userId') userId: string,
  ) {
    return this.aiService.dailyRecap(wsId, userId);
  }

  @Post('translate')
  async translate(
    @Workspace('id') wsId: string,
    @Body() dto: { message_id: string; target_language: string },
  ) {
    return this.aiService.translateMessage(wsId, dto.message_id, dto.target_language);
  }

  @Post('file-summary/:fileId')
  async fileSummary(
    @Param('fileId') fileId: string,
    @Workspace('id') wsId: string,
    @CurrentUser('userId') userId: string,
  ) {
    return this.aiService.fileSummary(fileId, wsId, userId);
  }

  // ============ FASE 5 — Embeddings ============

  @Post('embeddings/message/:messageId')
  async generateEmbedding(
    @Param('messageId') messageId: string,
    @Workspace('id') _wsId: string,
    @CurrentUser('userId') _userId: string,
  ) {
    return this.embeddingsService.generateForMessage(messageId);
  }

  @Post('embeddings/channel/:channelId')
  async generateChannelEmbeddings(
    @Param('channelId') channelId: string,
    @Workspace('id') wsId: string,
    @CurrentUser('userId') _userId: string,
    @Body() dto: { limit?: number; force?: boolean },
  ) {
    return this.embeddingsService.generateForChannel(channelId, wsId, dto);
  }

  @Post('embeddings/workspace')
  async generateWorkspaceEmbeddings(
    @Workspace('id') wsId: string,
    @CurrentUser('userId') _userId: string,
    @Body() dto: { limit?: number },
  ) {
    return this.embeddingsService.generateForWorkspace(wsId, dto);
  }

  @Delete('embeddings/message/:messageId')
  async deleteEmbedding(
    @Param('messageId') messageId: string,
    @Workspace('id') _wsId: string,
  ) {
    return this.embeddingsService.deleteEmbedding(messageId);
  }

  @Get('embeddings/stats')
  async embeddingStats(
    @Workspace('id') wsId: string,
  ) {
    return this.embeddingsService.getStats(wsId);
  }

  // ============ FASE 5 — AI Search ============

  @Post('search')
  async search(
    @Workspace('id') wsId: string,
    @CurrentUser('userId') userId: string,
    @Body() dto: { query: string; channel_id?: string; limit?: number },
  ) {
    return this.aiSearchService.search(wsId, userId, dto.query, {
      channelId: dto.channel_id,
      limit: dto.limit,
    });
  }

  @Get('search/history')
  async searchHistory(
    @Workspace('id') wsId: string,
    @CurrentUser('userId') userId: string,
    @Query('limit') limit?: number,
  ) {
    return this.aiSearchService.getHistory(wsId, userId, limit ? parseInt(limit as any) : 20);
  }

  // ============ FASE 5 — Workflow Generation ============

  @Post('workflows/generate')
  async generateWorkflowDraft(
    @Workspace('id') wsId: string,
    @CurrentUser('userId') userId: string,
    @Body() dto: { description: string },
  ) {
    return this.workflowGenerationService.generateDraft(wsId, userId, dto.description);
  }

  @Post('workflows/generate-save')
  async generateAndSaveWorkflow(
    @Workspace('id') wsId: string,
    @CurrentUser('userId') userId: string,
    @Body() dto: { description: string },
  ) {
    return this.workflowGenerationService.generateAndSave(wsId, userId, dto.description);
  }

  @Post('workflows/:workflowId/improve')
  async improveWorkflow(
    @Param('workflowId') workflowId: string,
    @Workspace('id') wsId: string,
    @CurrentUser('userId') userId: string,
    @Body() dto: { feedback: string },
  ) {
    return this.workflowGenerationService.improveWorkflow(wsId, workflowId, userId, dto.feedback);
  }

  // ============ FASE 5 — Huddle Notes (transcripción + resumen) ============

  @Post('huddles/:huddleId/notes/generate')
  async generateHuddleNotes(
    @Param('huddleId') huddleId: string,
    @Workspace('id') wsId: string,
    @CurrentUser('userId') userId: string,
    @Body() dto: { transcript?: string; generate_transcript?: boolean },
  ) {
    return this.huddleNotesService.generateNotes(huddleId, wsId, userId, {
      transcript: dto.transcript,
      generateTranscript: dto.generate_transcript,
    });
  }

  @Get('huddles/:huddleId/notes')
  async listHuddleNotes(
    @Param('huddleId') huddleId: string,
    @Workspace('id') wsId: string,
  ) {
    return this.huddleNotesService.listNotes(huddleId, wsId);
  }

  @Get('huddles/:huddleId/notes/:noteId')
  async getHuddleNote(
    @Param('huddleId') huddleId: string,
    @Param('noteId') noteId: string,
    @Workspace('id') wsId: string,
  ) {
    return this.huddleNotesService.getNote(huddleId, noteId, wsId);
  }
}