package com.cameleer.server.app.outbound.config;

import com.cameleer.server.app.license.LicenseEnforcer;
import com.cameleer.server.app.outbound.OutboundConnectionServiceImpl;
import com.cameleer.server.app.outbound.SsrfGuard;
import com.cameleer.server.app.outbound.crypto.SecretCipher;
import com.cameleer.server.app.outbound.storage.PostgresOutboundConnectionRepository;
import com.cameleer.server.core.alerting.AlertRuleRepository;
import com.cameleer.server.core.outbound.OutboundConnectionRepository;
import com.cameleer.server.core.outbound.OutboundConnectionService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
public class OutboundBeanConfig {

    @Bean
    public OutboundConnectionRepository outboundConnectionRepository(JdbcTemplate jdbc, ObjectMapper mapper) {
        return new PostgresOutboundConnectionRepository(jdbc, mapper);
    }

    @Bean
    public SecretCipher secretCipher(
            @Value("${cameleer.server.security.jwtsecret:dev-default-jwt-secret-do-not-use-in-production}")
            String jwtSecret) {
        return new SecretCipher(jwtSecret);
    }

    @Bean
    public OutboundConnectionService outboundConnectionService(
            OutboundConnectionRepository repo,
            AlertRuleRepository ruleRepo,
            SsrfGuard ssrfGuard,
            LicenseEnforcer licenseEnforcer,
            @Value("${cameleer.server.tenant.id:default}") String tenantId) {
        return new OutboundConnectionServiceImpl(repo, ruleRepo, ssrfGuard, licenseEnforcer, tenantId);
    }
}
