package auctiora;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Singleton;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;

import java.io.IOException;
import java.sql.SQLException;

/**
 * CDI Producer for auction monitor services.
 * Creates and configures singleton instances of core services.
 */
@ApplicationScoped
public class AuctionMonitorProducer {

    private static final Logger LOG = Logger.getLogger(AuctionMonitorProducer.class);

    @Produces
    @Singleton
    public DatabaseService produceDatabaseService(
            @ConfigProperty(name = "auction.database.path") String dbPath) throws SQLException {

        LOG.infof("Initializing DatabaseService with path: %s", dbPath);
       var db = new DatabaseService(dbPath);
        db.ensureSchema();
        return db;
    }

    @Produces
    @Singleton
    public NotificationService produceNotificationService(
            @ConfigProperty(name = "auction.notification.config") String config) {

        LOG.infof("Initializing NotificationService with config: %s", config);
        return new NotificationService(config);
    }

    @Produces
    @Singleton
    public ObjectDetectionService produceObjectDetectionService(
            @ConfigProperty(name = "auction.yolo.config") String cfgPath,
            @ConfigProperty(name = "auction.yolo.weights") String weightsPath,
            @ConfigProperty(name = "auction.yolo.classes") String classesPath) throws IOException {

        LOG.infof("Initializing ObjectDetectionService");
        return new ObjectDetectionService(cfgPath, weightsPath, classesPath);
    }

    @Produces
    @Singleton
    public ImageProcessingService produceImageProcessingService(
            DatabaseService db,
            ObjectDetectionService detector,
            RateLimitedHttpClient httpClient) {

        LOG.infof("Initializing ImageProcessingService");
        return new ImageProcessingService(db, detector, httpClient);
    }
}
