package org.mockserver.lifecycle;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import org.mockserver.configuration.Configuration;
import org.mockserver.log.MockServerEventLog;
import org.mockserver.log.model.LogEntry;
import org.mockserver.logging.MockServerLogger;
import org.mockserver.mock.HttpState;
import org.mockserver.mock.listeners.MockServerMatcherNotifier;
import org.mockserver.scheduler.Scheduler;
import org.mockserver.stop.Stoppable;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.mockserver.configuration.Configuration.configuration;
import static org.mockserver.log.model.LogEntry.LogMessageType.SERVER_CONFIGURATION;
import static org.mockserver.mock.HttpState.setPort;
import static org.slf4j.event.Level.*;

/**
 * @author jamesdbloom
 */
public abstract class LifeCycle implements Stoppable {

    protected final MockServerLogger mockServerLogger;
    protected final EventLoopGroup bossGroup;
    protected final EventLoopGroup workerGroup;
    protected final HttpState httpState;
    private final Configuration configuration;
    protected ServerBootstrap serverServerBootstrap;
    private final List<Future<Channel>> serverChannelFutures = new ArrayList<>();
    private final CompletableFuture<String> stopFuture = new CompletableFuture<>();
    private final AtomicBoolean stopping = new AtomicBoolean(false);
    private final Scheduler scheduler;
    // optional OTel exporters — null unless the corresponding config is enabled
    private final org.mockserver.metrics.OtelMetricsExporter otelMetricsExporter;
    private final org.mockserver.telemetry.GenAiSpanExporter genAiSpanExporter;

    protected LifeCycle(Configuration configuration) {
        this.configuration = configuration != null ? configuration : configuration();
        this.mockServerLogger = new MockServerLogger(this.configuration, MockServerEventLog.class);
        if (this.configuration.logEventListener() != null) {
            MockServerLogger.setGlobalLogEventListener(this.configuration.logEventListener());
        }
        this.bossGroup = new NioEventLoopGroup(5, new Scheduler.SchedulerThreadFactory(this.getClass().getSimpleName() + "-bossEventLoop"));
        this.workerGroup = new NioEventLoopGroup(this.configuration.nioEventLoopThreadCount(), new Scheduler.SchedulerThreadFactory(this.getClass().getSimpleName() + "-workerEventLoop"));
        this.scheduler = new Scheduler(this.configuration, this.mockServerLogger);
        this.httpState = new HttpState(this.configuration, this.mockServerLogger, this.scheduler);
        this.otelMetricsExporter = org.mockserver.metrics.OtelMetricsExporter.startIfEnabled();
        this.genAiSpanExporter = org.mockserver.telemetry.GenAiSpanExporter.startIfEnabled();
        installSemanticMatchingIfEnabled(this.workerGroup);
        installLlmCompletionServiceIfAvailable(this.workerGroup);
        installSemanticDriftIfEnabled(this.workerGroup);
        installPerformanceDriftThreshold();
    }

    /**
     * Install the opt-in semantic prompt matcher only when explicitly enabled and
     * a runtime LLM backend resolves. Off by default — the deterministic matcher
     * is never affected unless both conditions hold. Fail-soft.
     */
    private void installSemanticMatchingIfEnabled(EventLoopGroup eventLoopGroup) {
        if (!org.mockserver.configuration.ConfigurationProperties.llmSemanticMatchingEnabled()) {
            return;
        }
        try {
            java.util.Optional<org.mockserver.llm.client.LlmBackend> backend =
                new org.mockserver.llm.client.LlmBackendResolver().resolveDefault();
            if (!backend.isPresent()) {
                return;
            }
            org.mockserver.httpclient.NettyHttpClient httpClient =
                new org.mockserver.httpclient.NettyHttpClient(configuration, mockServerLogger, eventLoopGroup, null, false);
            org.mockserver.llm.client.LlmCompletionService service =
                new org.mockserver.llm.client.LlmCompletionService(new org.mockserver.llm.client.NettyHttpClientLlmTransport(httpClient));
            org.mockserver.llm.semantic.SemanticMatching.install(
                new org.mockserver.llm.semantic.SemanticPromptMatcher(service, backend.get()));
            org.slf4j.LoggerFactory.getLogger(LifeCycle.class)
                .info("semantic prompt matching enabled (backend: {})", backend.get().provider());
        } catch (Exception e) {
            // fail-soft — semantic matching stays off
            org.slf4j.LoggerFactory.getLogger(LifeCycle.class)
                .warn("failed to enable semantic prompt matching ({}); continuing without it", e.getMessage());
        }
    }

    /**
     * Wire a shared {@link org.mockserver.llm.client.LlmCompletionService} into
     * {@link org.mockserver.mock.HttpState} so the {@code /generateExpectation}
     * endpoint can call the configured LLM backend. When no backend is available
     * the endpoint falls back to template-based stubs. Fail-soft.
     */
    private void installLlmCompletionServiceIfAvailable(EventLoopGroup eventLoopGroup) {
        try {
            java.util.Optional<org.mockserver.llm.client.LlmBackend> backend =
                new org.mockserver.llm.client.LlmBackendResolver().resolveDefault();
            if (!backend.isPresent()) {
                return;
            }
            org.mockserver.httpclient.NettyHttpClient httpClient =
                new org.mockserver.httpclient.NettyHttpClient(configuration, mockServerLogger, eventLoopGroup, null, false);
            org.mockserver.llm.client.LlmCompletionService service =
                new org.mockserver.llm.client.LlmCompletionService(new org.mockserver.llm.client.NettyHttpClientLlmTransport(httpClient));
            httpState.setLlmCompletionService(service, backend.get());
            org.slf4j.LoggerFactory.getLogger(LifeCycle.class)
                .info("LLM completion service installed for stub generation (backend: {})", backend.get().provider());
        } catch (Exception e) {
            // fail-soft — stub generation endpoint will use template fallback
            org.slf4j.LoggerFactory.getLogger(LifeCycle.class)
                .warn("failed to install LLM completion service ({}); stub generation will use template fallback", e.getMessage());
        }
    }

    /**
     * When semantic drift analysis is enabled and a runtime LLM backend resolves,
     * create a {@link org.mockserver.mock.drift.SemanticDriftExtension} and install
     * it on the global {@link org.mockserver.mock.drift.DriftAnalyzer}. Fail-soft.
     */
    private void installSemanticDriftIfEnabled(EventLoopGroup eventLoopGroup) {
        if (!configuration.driftSemanticAnalysisEnabled()) {
            return;
        }
        try {
            java.util.Optional<org.mockserver.llm.client.LlmBackend> backend =
                new org.mockserver.llm.client.LlmBackendResolver().resolveDefault();
            if (!backend.isPresent()) {
                org.slf4j.LoggerFactory.getLogger(LifeCycle.class)
                    .info("semantic drift analysis enabled but no LLM backend available; feature disabled");
                return;
            }
            org.mockserver.httpclient.NettyHttpClient httpClient =
                new org.mockserver.httpclient.NettyHttpClient(configuration, mockServerLogger, eventLoopGroup, null, false);
            org.mockserver.llm.client.LlmCompletionService service =
                new org.mockserver.llm.client.LlmCompletionService(new org.mockserver.llm.client.NettyHttpClientLlmTransport(httpClient));
            org.mockserver.mock.drift.SemanticDriftExtension extension =
                new org.mockserver.mock.drift.SemanticDriftExtension(service, backend.get());
            org.mockserver.mock.drift.DriftAnalyzer.getInstance().setSemanticExtension(extension);
            org.slf4j.LoggerFactory.getLogger(LifeCycle.class)
                .info("semantic drift analysis enabled (backend: {})", backend.get().provider());
        } catch (Exception e) {
            // fail-soft — semantic drift analysis stays off
            org.slf4j.LoggerFactory.getLogger(LifeCycle.class)
                .warn("failed to enable semantic drift analysis ({}); continuing without it", e.getMessage());
        }
    }

    /**
     * Apply the configured p95 response time threshold for performance drift detection.
     */
    private void installPerformanceDriftThreshold() {
        long threshold = configuration.driftResponseTimeThresholdMs();
        if (threshold > 0) {
            org.mockserver.mock.drift.DriftAnalyzer.getInstance().setResponseTimeThresholdMs(threshold);
            org.slf4j.LoggerFactory.getLogger(LifeCycle.class)
                .info("performance drift detection enabled (p95 threshold: {} ms)", threshold);
        }
    }

    public CompletableFuture<String> stopAsync() {
        if (!stopFuture.isDone() && stopping.compareAndSet(false, true)) {
            final String message = "stopped for port" + (getLocalPorts().size() == 1 ? ": " + getLocalPorts().get(0) : "s: " + getLocalPorts());
            if (mockServerLogger != null && mockServerLogger.isEnabledForInstance(INFO)) {
                mockServerLogger.logEvent(
                    new LogEntry()
                        .setType(SERVER_CONFIGURATION)
                        .setLogLevel(INFO)
                        .setMessageFormat(message)
                );
            }
            new Scheduler.SchedulerThreadFactory("Stop").newThread(() -> {
                List<ChannelFuture> collect = serverChannelFutures
                    .stream()
                    .flatMap(channelFuture -> {
                        try {
                            return Stream.of(channelFuture.get());
                        } catch (Throwable throwable) {
                            // ignore
                            throwable.printStackTrace();
                            return Stream.empty();
                        }
                    })
                    .map(ChannelOutboundInvoker::disconnect)
                    .collect(Collectors.toList());
                try {
                    for (ChannelFuture channelFuture : collect) {
                        channelFuture.get();
                    }
                } catch (Throwable throwable) {
                    // ignore
                    throwable.printStackTrace();
                }

                httpState.stop();
                scheduler.shutdown();
                if (otelMetricsExporter != null) {
                    otelMetricsExporter.stop();
                }
                if (genAiSpanExporter != null) {
                    genAiSpanExporter.stop();
                }
                org.mockserver.llm.semantic.SemanticMatching.clear();

                // Shut down all event loops to terminate all threads.
                bossGroup.shutdownGracefully(5, 5, MILLISECONDS);
                workerGroup.shutdownGracefully(5, 5, MILLISECONDS);

                // Wait until all threads are terminated.
                bossGroup.terminationFuture().syncUninterruptibly();
                workerGroup.terminationFuture().syncUninterruptibly();

                stopFuture.complete(message);
            }).start();
        }
        return stopFuture;
    }

    public void stop() {
        try {
            stopAsync().get(10, SECONDS);
        } catch (Throwable throwable) {
            if (mockServerLogger != null && mockServerLogger.isEnabledForInstance(DEBUG)) {
                mockServerLogger.logEvent(
                    new LogEntry()
                        .setLogLevel(DEBUG)
                        .setMessageFormat("exception while stopping - " + throwable.getMessage())
                        .setArguments(throwable)
                );
            }
        }
    }

    @Override
    public void close() {
        stop();
    }

    protected EventLoopGroup getEventLoopGroup() {
        return workerGroup;
    }

    public Scheduler getScheduler() {
        return scheduler;
    }

    public boolean isRunning() {
        return !bossGroup.isShuttingDown() || !workerGroup.isShuttingDown();
    }

    public List<Integer> getLocalPorts() {
        return getBoundPorts(serverChannelFutures);
    }

    /**
     * @deprecated use getLocalPort instead of getPort
     */
    @Deprecated
    public Integer getPort() {
        return getLocalPort();
    }

    public int getLocalPort() {
        return getFirstBoundPort(serverChannelFutures);
    }

    private Integer getFirstBoundPort(List<Future<Channel>> channelFutures) {
        for (Future<Channel> channelOpened : channelFutures) {
            try {
                return ((InetSocketAddress) channelOpened.get(15, SECONDS).localAddress()).getPort();
            } catch (Throwable throwable) {
                if (mockServerLogger != null && mockServerLogger.isEnabledForInstance(WARN)) {
                    mockServerLogger.logEvent(
                        new LogEntry()
                            .setLogLevel(WARN)
                            .setMessageFormat("exception while retrieving port from channel future, ignoring port for this channel - " + throwable.getMessage())
                            .setArguments(throwable)
                    );
                }
            }
        }
        return -1;
    }

    private List<Integer> getBoundPorts(List<Future<Channel>> channelFutures) {
        List<Integer> ports = new ArrayList<>();
        for (Future<Channel> channelOpened : channelFutures) {
            try {
                ports.add(((InetSocketAddress) channelOpened.get(3, SECONDS).localAddress()).getPort());
            } catch (Exception e) {
                if (mockServerLogger != null && mockServerLogger.isEnabledForInstance(DEBUG)) {
                    mockServerLogger.logEvent(
                        new LogEntry()
                            .setLogLevel(DEBUG)
                            .setMessageFormat("exception while retrieving port from channel future, ignoring port for this channel")
                            .setArguments(e)
                    );
                }
            }
        }
        return ports;
    }

    public List<Integer> bindServerPorts(final List<Integer> requestedPortBindings) {
        return bindPorts(serverServerBootstrap, requestedPortBindings, serverChannelFutures);
    }

    private List<Integer> bindPorts(final ServerBootstrap serverBootstrap, List<Integer> requestedPortBindings, List<Future<Channel>> channelFutures) {
        List<Integer> actualPortBindings = new ArrayList<>();
        final String localBoundIP = configuration.localBoundIP();
        for (final Integer portToBind : requestedPortBindings) {
            try {
                final CompletableFuture<Channel> channelOpened = new CompletableFuture<>();
                channelFutures.add(channelOpened);
                new Scheduler.SchedulerThreadFactory("MockServer thread for port: " + portToBind, false).newThread(() -> {
                    try {
                        InetSocketAddress inetSocketAddress;
                        if (isBlank(localBoundIP)) {
                            inetSocketAddress = new InetSocketAddress(portToBind);
                        } else {
                            inetSocketAddress = new InetSocketAddress(localBoundIP, portToBind);
                        }
                        serverBootstrap
                            .bind(inetSocketAddress)
                            .addListener((ChannelFutureListener) future -> {
                                if (future.isSuccess()) {
                                    channelOpened.complete(future.channel());
                                } else {
                                    channelOpened.completeExceptionally(future.cause());
                                }
                            })
                            .channel().closeFuture().syncUninterruptibly();

                    } catch (Exception e) {
                        channelOpened.completeExceptionally(new RuntimeException("Exception while binding MockServer to port " + portToBind, e));
                    }
                }).start();

                actualPortBindings.add(((InetSocketAddress) channelOpened.get(configuration.maxFutureTimeoutInMillis(), MILLISECONDS).localAddress()).getPort());
            } catch (Exception e) {
                throw new RuntimeException("Exception while binding MockServer to port " + portToBind, e instanceof ExecutionException ? e.getCause() : e);
            }
        }
        return actualPortBindings;
    }

    protected void startedServer(List<Integer> ports) {
        final String message = "started on port" + (ports.size() == 1 ? ": " + ports.get(0) : "s: " + ports);
        setPort(ports);
        if (mockServerLogger != null && mockServerLogger.isEnabledForInstance(INFO)) {
            mockServerLogger.logEvent(
                new LogEntry()
                    .setType(SERVER_CONFIGURATION)
                    .setLogLevel(INFO)
                    .setMessageFormat(message)
            );
        }
    }

    public LifeCycle registerListener(ExpectationsListener expectationsListener) {
        httpState.getRequestMatchers().registerListener((requestMatchers, cause) -> {
            if (cause == MockServerMatcherNotifier.Cause.API) {
                expectationsListener.updated(requestMatchers.retrieveActiveExpectations(null));
            }
        });
        return this;
    }

}
