import { Controller, Get, Post, Body, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
import { VectorService } from './vector.service';
import { AdminAuthGuard } from '../../common/guards/admin-auth.guard';
import { AdminRolesGuard } from '../../common/guards/admin-roles.guard';
import { AdminRoles } from '../../common/decorators/admin-roles.decorator';
import type { AdminRole } from '../../common/types/admin-role.enum';

@ApiTags('admin-vector')
@Controller('admin-api/vector')
@UseGuards(AdminAuthGuard, AdminRolesGuard)
@ApiBearerAuth()
export class AdminVectorController {
  constructor(private readonly vector: VectorService) {}

  @Get('collection')
  @AdminRoles('SUPER_ADMIN' as AdminRole)
  @ApiOperation({ summary: 'Qdrant Collection 状态' })
  async collectionInfo() {
    return this.vector.getCollectionInfo();
  }

  @Get('count')
  @AdminRoles('SUPER_ADMIN' as AdminRole)
  @ApiOperation({ summary: '向量总数' })
  async count() {
    const count = await this.vector.count();
    return { collection: 'zhixi_chunks', count };
  }

  @Post('reindex')
  @AdminRoles('SUPER_ADMIN' as AdminRole)
  @ApiOperation({ summary: '触发索引重建（预留）' })
  async reindex(@Query('knowledgeBaseId') kbId?: string) {
    return { message: '索引重建已提交到队列', knowledgeBaseId: kbId || 'all' };
  }

  @Post('debug-search')
  @AdminRoles('SUPER_ADMIN' as AdminRole)
  @ApiOperation({ summary: 'RAG 检索调试' })
  async debugSearch(@Body() dto: { query: string; kbId?: string }) {
    // Simplified RAG debug — returns search parameters, actual search requires embedding
    return {
      query: dto.query,
      kbId: dto.kbId || 'all',
      note: 'RAG debug tool ready. Full search pipeline available in M3 with AI Gateway embedding.',
      filters: { knowledgeBaseId: dto.kbId, mustNotDeleted: true },
    };
  }
}
