package com.cloudhandson.vpdbackoffice.service;

import com.cloudhandson.vpdbackoffice.config.BackofficeProperties;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;

@Service
public class GameCatalogVectorService {
  private final BackofficeProperties properties;
  private final BearerTokenService bearerTokenService;

  public GameCatalogVectorService(BackofficeProperties properties, BearerTokenService bearerTokenService) {
    this.properties = properties;
    this.bearerTokenService = bearerTokenService;
  }

  public List<GameCandidate> search(String token, String question, int topK) {
    if (token == null || token.isBlank() || bearerTokenService.findByPlainToken(token.trim()) == null) {
      throw new VpdTokenAccessDeniedException();
    }
    BackofficeProperties.SelectAi db = properties.selectAi();
    if (db == null || !db.configured()) throw new AppException("게임 카탈로그 DB 설정이 필요합니다.");
    List<GameCandidate> result = new ArrayList<>();
    try (Connection c = DriverManager.getConnection(db.dbUrl(), db.dbUsername(), db.dbPassword());
         CallableStatement s = c.prepareCall("{ ? = call sg_game_catalog_search(?, ?) }")) {
      s.registerOutParameter(1, Types.REF_CURSOR); s.setString(2, question); s.setInt(3, Math.min(Math.max(topK, 1), 20)); s.execute();
      try (ResultSet rs = (ResultSet) s.getObject(1)) {
        while (rs.next()) result.add(new GameCandidate(rs.getString("GAME_KEY"), rs.getString("GAME_ID"), rs.getString("GAME_PREFIX"), rs.getString("GAME_NM"), rs.getString("GAME_ALIAS_NM"), rs.getDouble("COSINE_DISTANCE")));
      }
      return result;
    } catch (SQLException e) { throw new AppException("게임 카탈로그 벡터 검색 실패: " + e.getMessage()); }
  }

  public record GameCandidate(String gameKey, String gameId, String gamePrefix, String gameName, String aliases, double similarity) {}
}
