import { Controller, Post, Get, Body, Inject, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiResponse } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { AiChatService } from '../../../application/services/ai-chat.service';
import { AiCreditService } from '../../../application/services/ai-credit.service';
import { AiPricingService } from '../../../application/services/ai-pricing.service';
import { AiAnomalyService } from '../../../application/services/ai-anomaly.service';
import { ChatRequestDto, ChatResponseDto } from '../dto/chat.dto';
import { CreditScoreRequestDto, CreditScoreResponseDto } from '../dto/credit-score.dto';
import { PricingRequestDto, PricingResponseDto } from '../dto/pricing.dto';
import { AnomalyDetectRequestDto, AnomalyDetectResponseDto } from '../dto/anomaly.dto';
import {
  AI_AGENT_CLIENT,
  IAiAgentClient,
} from '../../../domain/ports/ai-agent.client.interface';

@ApiTags('AI')
@Controller('ai')
export class AiController {
  constructor(
    private readonly chatService: AiChatService,
    private readonly creditService: AiCreditService,
    private readonly pricingService: AiPricingService,
    private readonly anomalyService: AiAnomalyService,
    @Inject(AI_AGENT_CLIENT) private readonly agentClient: IAiAgentClient,
  ) {}

  @Post('chat')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Chat with AI assistant' })
  @ApiResponse({ status: 200, description: 'Chat response', type: ChatResponseDto })
  async chat(@Body() body: ChatRequestDto) {
    return { code: 0, data: await this.chatService.chat(body) };
  }

  @Post('credit/score')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get AI credit score' })
  @ApiResponse({ status: 200, description: 'Credit score result', type: CreditScoreResponseDto })
  async creditScore(@Body() body: CreditScoreRequestDto) {
    return { code: 0, data: await this.creditService.getScore(body) };
  }

  @Post('pricing/suggest')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get AI pricing suggestion' })
  @ApiResponse({ status: 200, description: 'Pricing suggestion', type: PricingResponseDto })
  async pricingSuggestion(@Body() body: PricingRequestDto) {
    return { code: 0, data: await this.pricingService.getSuggestion(body) };
  }

  @Post('anomaly/check')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Check for anomalous activity' })
  @ApiResponse({ status: 200, description: 'Anomaly check result', type: AnomalyDetectResponseDto })
  async anomalyCheck(@Body() body: AnomalyDetectRequestDto) {
    return { code: 0, data: await this.anomalyService.check(body) };
  }

  @Get('sessions/current/messages')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get current session chat messages' })
  async getSessionMessages() {
    return { code: 0, data: { messages: [] } };
  }

  @Get('health')
  @ApiOperation({ summary: 'AI service health + external agent status' })
  async health() {
    const agentHealthy = await this.agentClient.healthCheck();
    return {
      code: 0,
      data: {
        service: 'ai-service',
        status: 'ok',
        externalAgent: agentHealthy ? 'connected' : 'unavailable',
      },
    };
  }
}
