package com.infoepoch.pms.agent.config;

import com.infoepoch.pms.agent.observability.ai.AiModelInvoker;
import com.infoepoch.pms.agent.observability.ai.AiObservationFilter;
import com.infoepoch.pms.agent.observability.ai.AiObservationLogger;
import com.infoepoch.pms.agent.observability.ai.AiObservationSupport;
import com.infoepoch.pms.agent.observability.ai.ObservedReactAgentFactory;
import com.infoepoch.pms.agent.observability.ai.ObservedRestClientFactory;
import com.infoepoch.pms.agent.observability.ai.hook.AiAgentLifecycleObservationHook;
import com.infoepoch.pms.agent.observability.ai.hook.AiModelMessageObservationHook;
import com.infoepoch.pms.agent.properties.AiObservabilityProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * AI 观测基础设施配置。
 */
@Configuration
public class AiObservabilityConfiguration {

    @Bean
    AiObservationSupport aiObservationSupport(AiObservabilityProperties properties,
                                              @Value("${spring.application.name:pms-agent}") String serviceName) {
        return new AiObservationSupport(properties, serviceName);
    }

    @Bean
    AiObservationLogger aiObservationLogger(AiObservationSupport support) {
        return new AiObservationLogger(support);
    }

    @Bean
    AiModelInvoker aiModelInvoker(AiObservationLogger logger, AiObservationSupport support) {
        return new AiModelInvoker(logger, support);
    }

    @Bean
    ObservedRestClientFactory observedRestClientFactory(AiObservationSupport support) {
        return new ObservedRestClientFactory(support);
    }

    @Bean
    AiAgentLifecycleObservationHook aiAgentLifecycleObservationHook(AiObservationLogger logger,
                                                                    AiObservationSupport support) {
        return new AiAgentLifecycleObservationHook(logger, support);
    }

    @Bean
    AiModelMessageObservationHook aiModelMessageObservationHook(AiObservationLogger logger,
                                                                AiObservationSupport support) {
        return new AiModelMessageObservationHook(logger, support);
    }

    @Bean
    ObservedReactAgentFactory observedReactAgentFactory(AiAgentLifecycleObservationHook agentHook,
                                                        AiModelMessageObservationHook modelHook) {
        return new ObservedReactAgentFactory(agentHook, modelHook);
    }

    @Bean
    FilterRegistrationBean<AiObservationFilter> aiObservationFilterRegistration(AiObservationSupport support) {
        FilterRegistrationBean<AiObservationFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new AiObservationFilter(support));
        registrationBean.addUrlPatterns("/api/agent/*");
        registrationBean.addUrlPatterns("/api/chat/*");
        registrationBean.addUrlPatterns("/api/admin/*");
        registrationBean.setOrder(Integer.MIN_VALUE);
        return registrationBean;
    }
}
