/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.camel.component.platform.http.springboot;

import org.apache.camel.CamelContext;
import org.apache.camel.component.platform.http.PlatformHttpComponent;
import org.apache.camel.component.platform.http.spi.PlatformHttpEngine;
import org.apache.camel.spring.boot.ComponentConfigurationProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.core.env.Environment;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.List;
import java.util.concurrent.Executor;

@Configuration(proxyBeanMethods = false)
@AutoConfigureAfter(name = { "org.apache.camel.component.servlet.springboot.PlatformHttpComponentAutoConfiguration",
        "org.apache.camel.component.servlet.springboot.PlatformHttpComponentConverter" })
@EnableConfigurationProperties({ComponentConfigurationProperties.class,PlatformHttpComponentConfiguration.class, WebMvcProperties.class})
public class SpringBootPlatformHttpAutoConfiguration {
    private static final Logger LOG = LoggerFactory.getLogger(SpringBootPlatformHttpAutoConfiguration.class);

    @Bean(name = "platform-http-engine")
    @ConditionalOnMissingBean(PlatformHttpEngine.class)
    public PlatformHttpEngine springBootPlatformHttpEngine(Environment env, List<Executor> executors) {
        Executor executor;

        if (executors != null && !executors.isEmpty()) {
            executors.forEach(e -> LOG.debug("Analyzing executor: {}", e.getClass().getName()));
            
            // Check if virtual threads are enabled
            boolean virtualThreadsEnabled = Boolean.parseBoolean(env.getProperty("spring.threads.virtual.enabled", "false"));
            
            executor = executors.stream()
                    .filter(e -> {
                        try {
                            return Class.forName("org.springframework.security.task.DelegatingSecurityContextAsyncTaskExecutor").isInstance(e);
                        } catch (ClassNotFoundException ex) {
                            // No problem, spring-security is not configured
                            return false;
                        }
                    }).findAny().orElseGet(() -> {
                        if (virtualThreadsEnabled) {
                            // Prefer SimpleAsyncTaskExecutor when virtual threads are enabled
                            return executors.stream()
                                    .filter(e -> e instanceof SimpleAsyncTaskExecutor)
                                    .findFirst()
                                    .orElseGet(() -> 
                                            executors.stream()
                                                    .filter(e -> e instanceof ThreadPoolTaskExecutor)
                                                    .findFirst()
                                                    .orElseThrow(() -> new RuntimeException("No SimpleAsyncTaskExecutor or ThreadPoolTaskExecutor configured"))
                                    );
                        } else {
                            // Traditional behavior: prefer ThreadPoolTaskExecutor
                            return executors.stream()
                                    .filter(e -> e instanceof ThreadPoolTaskExecutor || e instanceof SimpleAsyncTaskExecutor)
                                    .findFirst()
                                    .orElseThrow(() -> new RuntimeException("No ThreadPoolTaskExecutor, SimpleAsyncTaskExecutor or DelegatingSecurityContextAsyncTaskExecutor configured"));
                        }
                    });
        } else {
            throw new RuntimeException("No Executor configured");
        }

        if (Boolean.parseBoolean(env.getProperty("spring.threads.virtual.enabled", "false"))) {
            LOG.info("Virtual threads enabled - using executor: {} for platform-http", executor.getClass().getName());
        } else {
            LOG.debug("Using executor: {}", executor.getClass().getName());
        }
        int port = Integer.parseInt(env.getProperty("server.port", "8080"));
        return new SpringBootPlatformHttpEngine(port, executor);
    }

    @Bean
    public CamelRequestHandlerMapping platformHttpEngineRequestMapping(PlatformHttpEngine engine, ObjectProvider<CamelContext> camelContextProvider) {
        CamelContext camelContext = camelContextProvider.getObject();
        PlatformHttpComponent component = camelContext.getComponent("platform-http", PlatformHttpComponent.class);
        CamelRequestHandlerMapping answer = new CamelRequestHandlerMapping(component, engine);
        return answer;
    }

}
