package com.lanyuanxiaoyao.service.ai.knowledge.controller;

import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.ai.core.entity.amis.AmisMapResponse;
import com.lanyuanxiaoyao.service.ai.core.entity.amis.AmisResponse;
import com.lanyuanxiaoyao.service.ai.knowledge.entity.vo.SegmentVO;
import com.lanyuanxiaoyao.service.ai.knowledge.service.EmbeddingService;
import com.lanyuanxiaoyao.service.ai.knowledge.service.KnowledgeService;
import java.util.concurrent.ExecutionException;
import org.eclipse.collections.api.factory.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lanyuanxiaoyao
 * @version 20250515
 */
@RestController
@RequestMapping("knowledge")
public class KnowledgeController {
  private static final Logger logger = LoggerFactory.getLogger(KnowledgeController.class);

  private final KnowledgeService knowledgeService;
  private final EmbeddingService embeddingService;

  public KnowledgeController(KnowledgeService knowledgeService, EmbeddingService embeddingService) {
    this.knowledgeService = knowledgeService;
    this.embeddingService = embeddingService;
  }

  @PostMapping("add")
  public void add(
      @RequestParam("name") String name,
      @RequestParam("strategy") String strategy
  ) throws ExecutionException, InterruptedException {
    knowledgeService.add(name, strategy);
  }

  @GetMapping("name")
  public AmisMapResponse name(@RequestParam("id") Long id) {
    return AmisResponse.responseMapData()
        .setData("name", knowledgeService.getName(id));
  }

  @GetMapping("list")
  public AmisResponse<?> list() {
    return AmisResponse.responseCrudData(knowledgeService.list());
  }

  @GetMapping("delete")
  public void delete(@RequestParam("id") Long id) throws ExecutionException, InterruptedException {
    knowledgeService.remove(id);
  }

  @PostMapping("preview_text")
  public AmisResponse<?> previewText(
      @RequestParam(value = "mode", defaultValue = "NORMAL") String mode,
      @RequestParam(value = "type", defaultValue = "text") String type,
      @RequestParam(value = "content", required = false) String content,
      @RequestParam(value = "files", required = false) String files
  ) {
    if (StrUtil.equals("text", type)) {
      return AmisResponse.responseCrudData(
          embeddingService.preview(mode, content)
              .collect(doc -> {
                SegmentVO vo = new SegmentVO();
                vo.setId(doc.getId());
                vo.setText(doc.getText());
                return vo;
              })
      );
    } else if (StrUtil.equals("file", type)) {
      return AmisResponse.responseCrudData(
          embeddingService.preview(mode, Lists.immutable.of(files.split(",")))
              .collect(doc -> {
                SegmentVO vo = new SegmentVO();
                vo.setId(doc.getId());
                vo.setText(doc.getText());
                return vo;
              })
      );
    } else {
      throw new IllegalArgumentException("Unsupported type: " + type);
    }
  }

  @PostMapping("submit_text")
  public void submitText(
      @RequestParam(value = "id") Long id,
      @RequestParam(value = "mode", defaultValue = "NORMAL") String mode,
      @RequestParam(value = "type", defaultValue = "text") String type,
      @RequestParam(value = "content", required = false) String content,
      @RequestParam(value = "files", required = false) String files
  ) {
    if (StrUtil.equals("text", type)) {
      embeddingService.submit(id, mode, content);
    } else if (StrUtil.equals("file", type)) {
      embeddingService.submit(id, mode, Lists.immutable.of(files.split(",")));
    } else {
      throw new IllegalArgumentException("Unsupported type: " + type);
    }
  }
}
