import { Injectable, NotFoundException, BadRequestException, Optional } from '@nestjs/common';
import { KnowledgeSourceRepository } from './knowledge-source.repository';
import { DocumentImportRepository } from '../document-import/document-import.repository';
import { QueueService } from '../../infrastructure/queue/queue.service';
import { RedisService } from '../../infrastructure/redis/redis.service';
import { FilesService } from '../files/files.service';
import { StorageService } from '../../infrastructure/storage/storage.service';
import { KnowledgeItemsRepository } from '../knowledge-items/knowledge-items.repository';
import { KnowledgeImportWorkflow } from '../ai/workflows/knowledge-import.workflow';


@Injectable()
export class KnowledgeSourceService {
  constructor(
    private readonly repository: KnowledgeSourceRepository,
    private readonly importRepo: DocumentImportRepository,
    private readonly queue: QueueService,
    private readonly redis: RedisService,
    private readonly filesService: FilesService,
    @Optional() private readonly storage?: StorageService,
    @Optional() private readonly knowledgeItemsRepo?: KnowledgeItemsRepository,
    @Optional() private readonly knowledgeImportWorkflow?: KnowledgeImportWorkflow,

  ) {}

  async addSource(userId: string, knowledgeBaseId: string, dto: {
    fileId?: string;
    type?: string;
    title?: string;
    originalFilename?: string;
    mimeType?: string;
    sizeBytes?: number;
    originalObjectKey?: string;
  }) {
    const source = await this.repository.create(userId, knowledgeBaseId, dto);

    // 自动创建 import 任务并入队
    const importJob = await this.importRepo.create({
      userId,
      knowledgeBaseId,
      sourceId: source.id,
      fileId: dto.fileId,
      sourceType: dto.type ?? 'file',
      sourceName: dto.originalFilename ?? dto.title ?? '',
      status: 'QUEUED',
    });

    // 设置 Redis 状态
    await this.redis.set(`job:document-import:${importJob.id}:status`, 'pending', 86400);
    await this.redis.set(`job:document-import:${importJob.id}:progress`, '0', 86400);
    await this.redis.set(`job:document-import:${importJob.id}:message`, '任务已加入队列', 86400);

    // 入队处理
    await this.queue.add('document-import', {
      importId: importJob.id,
      userId,
      knowledgeBaseId,
      sourceId: source.id,
      fileName: dto.originalFilename ?? dto.title,
    });

    return source;
  }

  async findByKnowledgeBase(knowledgeBaseId: string, pagination: { page?: number; limit?: number; sortBy?: string; order?: string }) {
    return this.repository.findByKnowledgeBase(knowledgeBaseId, pagination);
  }

  async findOne(id: string) {
    const source = await this.repository.findById(id);
    if (!source) throw new NotFoundException('资料来源不存在');
    return source;
  }

  async remove(userId: string, id: string) {
    const source = await this.repository.findById(id);
    if (!source) throw new NotFoundException('资料来源不存在');
    if (source.fileId) {
      await this.filesService.deleteFile(userId, source.fileId);
    }
    return this.repository.softDelete(id);
  }


  /** 手动触发 AI 知识点生成（brain 图标）
   *  不经过 Python RAG Worker，直接读文件 + 调 AI Gateway
   */
  async generateKnowledgePoints(knowledgeBaseId: string, sourceId: string, userId: string) {
    const source = await this.repository.findById(sourceId);
    if (!source) throw new NotFoundException('资料来源不存在');

    const objectKey = source.originalObjectKey;
    if (!objectKey) throw new BadRequestException('文件不存在，无法生成知识点');

    if (!this.storage || !this.knowledgeItemsRepo || !this.knowledgeImportWorkflow) {
      throw new BadRequestException('知识点生成服务未就绪');
    }

    const buf = await this.storage.getObject(objectKey);
    if (!buf) throw new BadRequestException('无法读取文件内容');

    const text = buf.toString('utf-8');
    if (!text.trim()) throw new BadRequestException('文件内容为空，无法生成知识点');

    const result = await this.knowledgeImportWorkflow.execute({
      userId,
      rawText: text,
      sourceName: source.originalFilename ?? source.title ?? '',
    });

    if (!result?.knowledgePoints?.length) {
      return { items: [], count: 0, sourceId, message: 'AI 未识别到可提取的知识点' };
    }

    const items: any[] = [];
    for (let i = 0; i < result.knowledgePoints.length; i++) {
      const kp = result.knowledgePoints[i];
      const item = await this.knowledgeItemsRepo.create(userId, knowledgeBaseId, {
        title: kp.title,
        content: kp.content,
        itemType: 'lesson',
        orderIndex: kp.suggestedOrder ?? i + 1,
        sourceId,
        sourceRef: sourceId,
      });
      items.push(item);
    }

    return { items, count: items.length, sourceId };
  }

  async triggerParse(sourceId: string, userId: string) {
    const source = await this.repository.findById(sourceId);
    if (!source) throw new NotFoundException('资料来源不存在');

    // 创建新的 import 任务
    const importJob = await this.importRepo.create({
      userId,
      knowledgeBaseId: source.knowledgeBaseId,
      sourceId: source.id,
      sourceType: source.type ?? 'file',
      sourceName: source.originalFilename ?? source.title ?? '',
      status: 'QUEUED',
    });

    // 设置 Redis 状态
    await this.redis.set(`job:document-import:${importJob.id}:status`, 'pending', 86400);
    await this.redis.set(`job:document-import:${importJob.id}:progress`, '0', 86400);
    await this.redis.set(`job:document-import:${importJob.id}:message`, '手动触发重新解析', 86400);

    // 入队
    await this.queue.add('document-import', {
      importId: importJob.id,
      userId,
      knowledgeBaseId: source.knowledgeBaseId,
      sourceId: source.id,
      fileName: source.originalFilename ?? source.title,
    });

    return { id: importJob.id, status: 'queued' };
  }

  async updateParseStatus(id: string, parseStatus: string, data?: any) {
    return this.repository.updateParseStatus(id, parseStatus, data);
  }

  async updateIndexStatus(id: string, indexStatus: string, errorCode?: string, errorMessage?: string) {
    return this.repository.updateIndexStatus(id, indexStatus, errorCode, errorMessage);
  }
}
