package ai.decompile.job.service.handler;

import ai.decompile.job.model.enums.JobType;
import jakarta.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class JobHandlerFactory {
  private final List<JobHandler> handlerList;
  private Map<JobType, JobHandler> handlers;

  @PostConstruct
  void init() {
    this.handlers =
        handlerList.stream()
            .collect(Collectors.toMap(JobHandler::supportedType, Function.identity()));
  }

  public JobHandler getHandler(JobType type) {
    var handler = handlers.get(type);
    if (handler == null) {
      throw new IllegalStateException("No JobHandler registered for type: " + type);
    }

    return handler;
  }
}
