package com.cloudhandson.vpdbackoffice.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

/** Product-neutral labels and endpoint details for the MCP Select AI tool. */
@ConfigurationProperties(prefix = "backoffice.mcp")
public record McpProperties(
    String toolName,
    String toolLabel,
    String toolDescription,
    String promptDescription,
    String showpromptToolName,
    String showpromptToolLabel,
    String showpromptToolDescription,
    String qaVectorSearchToolName,
    String qaVectorSearchToolLabel,
    String qaVectorSearchToolDescription,
    String qaVectorStoreToolName,
    String qaVectorStoreToolLabel,
    String qaVectorStoreToolDescription,
    String fewShotNl2SqlToolName,
    String fewShotNl2SqlToolLabel,
    String fewShotNl2SqlToolDescription,
    String gameScopeToolName,
    String gameScopeToolLabel,
    String gameScopeToolDescription
) {

  private static final String DEFAULT_TOOL_NAME = "oracle.select_ai.data_text2sql";
  private static final String DEFAULT_TOOL_LABEL = "업무 데이터 Text2SQL";
  private static final String DEFAULT_TOOL_DESCRIPTION =
      "승인된 업무 데이터용 읽기 전용 SELECT/WITH SQL을 생성하고, 검증 후 읽기 전용 트랜잭션에서 실행합니다. "
          + "생성 SQL과 최대 100건의 조회 결과를 함께 반환하며 DDL/DML/잠금/패키지 호출은 실행하지 않습니다.";
  private static final String DEFAULT_PROMPT_DESCRIPTION =
      "업무 데이터에서 조회할 내용을 자연어로 입력합니다.";
  private static final String DEFAULT_SHOWPROMPT_TOOL_NAME =
      "oracle.select_ai.data_showprompt";
  private static final String DEFAULT_SHOWPROMPT_TOOL_LABEL =
      "업무 데이터 SHOWPROMPT";
  private static final String DEFAULT_SHOWPROMPT_TOOL_DESCRIPTION =
      "Select AI가 SQL 생성에 사용한 prompt를 조회하는 읽기 전용 진단 도구입니다. "
          + "생성 SQL이나 데이터 조회 SQL은 실행하지 않습니다.";
  private static final String DEFAULT_QA_VECTOR_SEARCH_TOOL_NAME =
      "oracle.select_ai.qa_vector_search";
  private static final String DEFAULT_QA_VECTOR_SEARCH_TOOL_LABEL = "Select AI 예제 SQL 조회";
  private static final String DEFAULT_QA_VECTOR_SEARCH_TOOL_DESCRIPTION =
      "현재 질문에 사용할 유사 예제 SQL을 Select AI 실행 전에 조회합니다. "
          + "반환값은 few-shot 컨텍스트 검토용이며 SQL을 실행하지 않습니다.";
  private static final String DEFAULT_QA_VECTOR_STORE_TOOL_NAME =
      "oracle.select_ai.qa_vector_store";
  private static final String DEFAULT_QA_VECTOR_STORE_TOOL_LABEL = "Select AI 예제 SQL 저장";
  private static final String DEFAULT_QA_VECTOR_STORE_TOOL_DESCRIPTION =
      "검토된 Select AI 결과를 후속 Text2SQL 품질 향상용 예제 SQL로 저장합니다. "
          + "질문과 읽기 전용 답 SQL이 필요합니다.";
  private static final String DEFAULT_FEW_SHOT_NL2SQL_TOOL_NAME =
      "oracle.select_ai.smilegate_fewshot_nl2sql";
  private static final String DEFAULT_FEW_SHOT_NL2SQL_TOOL_LABEL =
      "Few-shot NL2SQL 실행";
  private static final String DEFAULT_FEW_SHOT_NL2SQL_TOOL_DESCRIPTION =
      "벡터 Few-shot 예제를 찾아 prompt에 반영하고, SHOWSQL로 생성한 읽기 전용 SQL을 실행합니다. "
          + "Few-shot 근거, 생성 SQL, 실행 결과를 함께 반환합니다.";
  private static final String DEFAULT_GAME_SCOPE_TOOL_NAME =
      "oracle.select_ai.game_scope_resolve";
  private static final String DEFAULT_GAME_SCOPE_TOOL_LABEL = "게임 조회 범위 확인";
  private static final String DEFAULT_GAME_SCOPE_TOOL_DESCRIPTION =
      "질문에서 언급된 게임 별칭을 DB 게임 범위 view로 확인합니다. 데이터 SQL은 실행하지 않으며, "
          + "반환된 SUPPORTED scope에만 Few-shot NL2SQL을 호출하세요.";

  public String resolvedToolName() {
    return requiredOrDefault(toolName, DEFAULT_TOOL_NAME);
  }

  public String resolvedToolLabel() {
    return requiredOrDefault(toolLabel, DEFAULT_TOOL_LABEL);
  }

  public String resolvedToolDescription() {
    return requiredOrDefault(toolDescription, DEFAULT_TOOL_DESCRIPTION);
  }

  public String resolvedPromptDescription() {
    return requiredOrDefault(promptDescription, DEFAULT_PROMPT_DESCRIPTION);
  }

  public String resolvedShowpromptToolName() {
    return requiredOrDefault(showpromptToolName, DEFAULT_SHOWPROMPT_TOOL_NAME);
  }

  public String resolvedShowpromptToolLabel() {
    return requiredOrDefault(showpromptToolLabel, DEFAULT_SHOWPROMPT_TOOL_LABEL);
  }

  public String resolvedShowpromptToolDescription() {
    return requiredOrDefault(showpromptToolDescription, DEFAULT_SHOWPROMPT_TOOL_DESCRIPTION);
  }

  public String resolvedQaVectorSearchToolName() {
    return requiredOrDefault(qaVectorSearchToolName, DEFAULT_QA_VECTOR_SEARCH_TOOL_NAME);
  }

  public String resolvedQaVectorSearchToolLabel() {
    return requiredOrDefault(qaVectorSearchToolLabel, DEFAULT_QA_VECTOR_SEARCH_TOOL_LABEL);
  }

  public String resolvedQaVectorSearchToolDescription() {
    return requiredOrDefault(qaVectorSearchToolDescription, DEFAULT_QA_VECTOR_SEARCH_TOOL_DESCRIPTION);
  }

  public String resolvedQaVectorStoreToolName() {
    return requiredOrDefault(qaVectorStoreToolName, DEFAULT_QA_VECTOR_STORE_TOOL_NAME);
  }

  public String resolvedQaVectorStoreToolLabel() {
    return requiredOrDefault(qaVectorStoreToolLabel, DEFAULT_QA_VECTOR_STORE_TOOL_LABEL);
  }

  public String resolvedQaVectorStoreToolDescription() {
    return requiredOrDefault(qaVectorStoreToolDescription, DEFAULT_QA_VECTOR_STORE_TOOL_DESCRIPTION);
  }

  public String resolvedFewShotNl2SqlToolName() {
    return requiredOrDefault(fewShotNl2SqlToolName, DEFAULT_FEW_SHOT_NL2SQL_TOOL_NAME);
  }

  public String resolvedFewShotNl2SqlToolLabel() {
    return requiredOrDefault(fewShotNl2SqlToolLabel, DEFAULT_FEW_SHOT_NL2SQL_TOOL_LABEL);
  }

  public String resolvedFewShotNl2SqlToolDescription() {
    return requiredOrDefault(fewShotNl2SqlToolDescription, DEFAULT_FEW_SHOT_NL2SQL_TOOL_DESCRIPTION);
  }

  public String resolvedGameScopeToolName() {
    return requiredOrDefault(gameScopeToolName, DEFAULT_GAME_SCOPE_TOOL_NAME);
  }

  public String resolvedGameScopeToolLabel() {
    return requiredOrDefault(gameScopeToolLabel, DEFAULT_GAME_SCOPE_TOOL_LABEL);
  }

  public String resolvedGameScopeToolDescription() {
    return requiredOrDefault(gameScopeToolDescription, DEFAULT_GAME_SCOPE_TOOL_DESCRIPTION);
  }

  private String requiredOrDefault(String value, String fallback) {
    return value == null || value.isBlank() ? fallback : value.trim();
  }
}
