package com.cloudhandson.vpdbackoffice.web;

import com.cloudhandson.vpdbackoffice.domain.vector.VectorIngestCommand;
import com.cloudhandson.vpdbackoffice.domain.vector.VectorIngestResult;
import com.cloudhandson.vpdbackoffice.service.VectorKnowledgeService;
import com.cloudhandson.vpdbackoffice.mapper.UserMapper;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class VectorKnowledgeController {

  private final VectorKnowledgeService vectorKnowledgeService;
  private final UserMapper userMapper;

  public VectorKnowledgeController(VectorKnowledgeService vectorKnowledgeService, UserMapper userMapper) {
    this.vectorKnowledgeService = vectorKnowledgeService;
    this.userMapper = userMapper;
  }

  @GetMapping("/vector-knowledge")
  public String page(Model model) {
    model.addAttribute("users", userMapper.findAll());
    model.addAttribute("aiEmbeddingConfigured", vectorKnowledgeService.aiEmbeddingConfigured());
    try {
      model.addAttribute("summary", vectorKnowledgeService.summary());
    } catch (DataAccessException exception) {
      RuntimeErrorMessage error = RuntimeErrorMessages.dataAccess(exception);
      model.addAttribute("summary", null);
      model.addAttribute("runtimeError", error);
    }
    return "vector-knowledge";
  }

  @PostMapping("/vector-knowledge/ingest")
  public String ingest(
      @RequestParam String documentId,
      @RequestParam String title,
      @RequestParam(required = false, defaultValue = "") String sourceUri,
      @RequestParam String content,
      @RequestParam String techTags,
      @RequestParam(defaultValue = "600") int chunkSize,
      @RequestParam(defaultValue = "DEMO") String embeddingMode,
      RedirectAttributes redirectAttributes
  ) {
    try {
      VectorIngestResult result = vectorKnowledgeService.ingest(new VectorIngestCommand(
          documentId, title, sourceUri, content, techTags, chunkSize, embeddingMode));
      redirectAttributes.addFlashAttribute("ingestResult", result);
      redirectAttributes.addFlashAttribute("message",
          result.chunkCount() + "개 청크를 저장했습니다. 태그 " + result.tagCount() + "개가 각 청크에 연결되었습니다.");
    } catch (Exception exception) {
      redirectAttributes.addFlashAttribute("errorMessage", exception.getMessage());
    }
    return "redirect:/vector-knowledge";
  }

  @PostMapping("/vector-knowledge/search")
  public String search(
      @RequestParam long userId,
      @RequestParam String query,
      @RequestParam(defaultValue = "10") int limit,
      @RequestParam(defaultValue = "DEMO") String embeddingMode,
      Model model
  ) {
    try {
      model.addAttribute("searchResult", vectorKnowledgeService.search(userId, query, limit, embeddingMode));
    } catch (Exception exception) {
      model.addAttribute("errorMessage", exception.getMessage());
    }
    return "fragments/vector-search-result :: result";
  }
}
