package ai.decompile.job.service.handler;

import ai.decompile.ai.service.EmbeddingService;
import ai.decompile.job.model.entity.Job;
import ai.decompile.job.model.enums.JobStatus;
import ai.decompile.job.model.enums.JobType;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

@Log4j2
@Component
@ConditionalOnProperty(name = "app.ai.enabled", havingValue = "true", matchIfMissing = true)
@RequiredArgsConstructor
public class EmbeddingGenerationHandler implements JobHandler {
  private final EmbeddingService embeddingService;

  @Override
  public void process(Job job) {
    log.info("Processing GENERATE_EMBEDDINGS job {} for binary {}", job.getId(), job.getBinaryId());

    var chunkCount = embeddingService.indexBinary(job.getBinaryId());

    job.setResult("{\"chunkCount\":" + chunkCount + "}");
    job.setStatus(JobStatus.COMPLETED);

    log.info(
        "Job {} completed. Generated {} embedding chunks for binary {}",
        job.getId(),
        chunkCount,
        job.getBinaryId());
  }

  @Override
  public JobType supportedType() {
    return JobType.GENERATE_EMBEDDINGS;
  }
}
