package ai.decompile.ai.service.tools;

import ai.decompile.analysis.model.entity.StaticAnalysis;
import ai.decompile.analysis.model.repository.StaticAnalysisRepository;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Log4j2
@Component
@RequiredArgsConstructor
@Transactional(readOnly = true)
@ConditionalOnProperty(name = "app.ai.enabled", havingValue = "true", matchIfMissing = true)
public class AnalysisResolver {
  private final StaticAnalysisRepository analysisRepository;

  public StaticAnalysis resolve(String binaryId) {
    UUID binaryUuid;

    try {
      binaryUuid = UUID.fromString(binaryId);
    } catch (IllegalArgumentException e) {
      log.warn("resolveAnalysis: invalid binaryId format '{}'", binaryId);
      return null;
    }

    return analysisRepository.findFirstByBinaryIdOrderByCreatedAtDesc(binaryUuid).orElse(null);
  }
}
