package com.cameleer.server.app.config;

import com.cameleer.server.app.analytics.UsageTrackingInterceptor;
import com.cameleer.server.app.interceptor.AuditInterceptor;
import com.cameleer.server.app.interceptor.ProtocolVersionInterceptor;
import com.cameleer.server.app.web.EnvironmentPathResolver;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * Web MVC configuration.
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    private final ProtocolVersionInterceptor protocolVersionInterceptor;
    private final AuditInterceptor auditInterceptor;
    private final UsageTrackingInterceptor usageTrackingInterceptor;
    private final EnvironmentPathResolver environmentPathResolver;

    public WebConfig(ProtocolVersionInterceptor protocolVersionInterceptor,
                     AuditInterceptor auditInterceptor,
                     @org.springframework.lang.Nullable UsageTrackingInterceptor usageTrackingInterceptor,
                     EnvironmentPathResolver environmentPathResolver) {
        this.protocolVersionInterceptor = protocolVersionInterceptor;
        this.auditInterceptor = auditInterceptor;
        this.usageTrackingInterceptor = usageTrackingInterceptor;
        this.environmentPathResolver = environmentPathResolver;
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(environmentPathResolver);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(protocolVersionInterceptor)
                .addPathPatterns("/api/v1/data/**", "/api/v1/agents/**")
                .excludePathPatterns(
                        "/api/v1/health",
                        "/api/v1/api-docs/**",
                        "/api/v1/swagger-ui/**",
                        "/api/v1/swagger-ui.html",
                        "/api/v1/agents/*/events",
                        "/api/v1/agents/register",
                        "/api/v1/agents/*/refresh"
                );

        // Usage analytics: tracks authenticated UI user requests
        if (usageTrackingInterceptor != null) {
            registry.addInterceptor(usageTrackingInterceptor)
                    .addPathPatterns("/api/v1/**")
                    .excludePathPatterns(
                            "/api/v1/data/**",
                            "/api/v1/agents/*/heartbeat",
                            "/api/v1/agents/*/events",
                            "/api/v1/health"
                    );
        }

        // Safety-net audit: catches any unaudited POST/PUT/DELETE
        registry.addInterceptor(auditInterceptor)
                .addPathPatterns("/api/v1/**")
                .excludePathPatterns(
                        "/api/v1/data/**",
                        "/api/v1/agents/*/heartbeat",
                        "/api/v1/health"
                );
    }
}
