import { Controller, Optional, Get, Post, Delete, Body, Param, Query } from '@nestjs/common';
import { ApiTags, ApiOperation } from '@nestjs/swagger';
import { KnowledgeSourceService } from './knowledge-source.service';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { AddSourceDto } from './dto/add-source.dto';
import type { UserPayload } from '../../common/types';

@ApiTags('knowledge-source')
@Controller('knowledge-bases/:kbId/sources')
export class KnowledgeSourceController {
  constructor(private readonly service: KnowledgeSourceService) {}

  @Post()
  @ApiOperation({ summary: '添加资料来源' })
  async addSource(
    @CurrentUser() user: UserPayload,
    @Param('kbId') kbId: string,
    @Body() dto: AddSourceDto,
  ) {
    return this.service.addSource(user.id, kbId, dto);
  }

  @Get()
  @ApiOperation({ summary: '获取知识库的资料列表' })
  async findAll(@Param('kbId') kbId: string, @Query() pagination: any) {
    return this.service.findByKnowledgeBase(kbId, pagination);
  }

  @Get(':id')
  @ApiOperation({ summary: '获取资料详情' })
  async findOne(@Param('id') id: string) {
    return this.service.findOne(id);
  }

  @Post(':id/parse')
  @ApiOperation({ summary: '手动触发资料解析' })
  async triggerParse(
    @CurrentUser() user: UserPayload,
    @Param('id') id: string,
  ) {
    return this.service.triggerParse(id, user.id);
  }


  @Post(':id/generate-knowledge')
  @ApiOperation({ summary: 'AI 生成知识点（brain 图标）' })
  async generateKnowledge(
    @CurrentUser() user: UserPayload,
    @Param('kbId') kbId: string,
    @Param('id') id: string,
  ) {
    return this.service.generateKnowledgePoints(kbId, id, user.id);
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除资料来源' })
  async remove(@CurrentUser() user: UserPayload, @Param('id') id: string) {
    return this.service.remove(user.id, id);
  }
}
