/*
 * 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.google.pubsub;

import java.io.IOException;
import java.time.Duration;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;

import com.google.api.core.AbstractApiService;
import com.google.api.core.ApiFuture;
import com.google.api.gax.rpc.ApiException;
import com.google.cloud.pubsub.v1.MessageReceiver;
import com.google.cloud.pubsub.v1.Subscriber;
import com.google.cloud.pubsub.v1.SubscriptionAdminClient;
import com.google.cloud.pubsub.v1.stub.SubscriberStub;
import com.google.common.base.Strings;
import com.google.pubsub.v1.DeadLetterPolicy;
import com.google.pubsub.v1.ProjectSubscriptionName;
import com.google.pubsub.v1.PubsubMessage;
import com.google.pubsub.v1.PullRequest;
import com.google.pubsub.v1.PullResponse;
import com.google.pubsub.v1.ReceivedMessage;
import com.google.pubsub.v1.Subscription;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.component.google.pubsub.consumer.AcknowledgeCompletion;
import org.apache.camel.component.google.pubsub.consumer.AcknowledgeSync;
import org.apache.camel.component.google.pubsub.consumer.CamelMessageReceiver;
import org.apache.camel.component.google.pubsub.consumer.GooglePubsubAcknowledge;
import org.apache.camel.spi.HeaderFilterStrategy;
import org.apache.camel.spi.Synchronization;
import org.apache.camel.support.DefaultConsumer;
import org.apache.camel.support.UnitOfWorkHelper;
import org.apache.camel.support.task.Tasks;
import org.apache.camel.support.task.budget.Budgets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GooglePubsubConsumer extends DefaultConsumer {

    private final Logger localLog;

    private final GooglePubsubEndpoint endpoint;
    private final Processor processor;
    private ExecutorService executor;
    private final List<Subscriber> subscribers;
    private final Set<ApiFuture<PullResponse>> pendingSynchronousPullResponses;
    private final HeaderFilterStrategy headerFilterStrategy;
    private int resolvedMaxDeliveryAttempts;

    GooglePubsubConsumer(GooglePubsubEndpoint endpoint, Processor processor) {
        super(endpoint, processor);
        this.endpoint = endpoint;
        this.processor = processor;
        this.subscribers = new CopyOnWriteArrayList<>();
        this.pendingSynchronousPullResponses = ConcurrentHashMap.newKeySet();
        String loggerId = endpoint.getLoggerId();

        if (Strings.isNullOrEmpty(loggerId)) {
            loggerId = this.getClass().getName();
        }

        localLog = LoggerFactory.getLogger(loggerId);
        headerFilterStrategy = endpoint.getHeaderFilterStrategy();
    }

    @Override
    protected void doStart() throws Exception {
        super.doStart();

        localLog.info("Starting Google PubSub consumer for {}/{}", endpoint.getProjectId(), endpoint.getDestinationName());

        resolvedMaxDeliveryAttempts = resolveMaxDeliveryAttempts();
        if (resolvedMaxDeliveryAttempts > 0) {
            localLog.info("Max delivery attempts enforcement enabled: {} for subscription {}",
                    resolvedMaxDeliveryAttempts, endpoint.getDestinationName());
        }

        executor = endpoint.createExecutor(this);
        for (int i = 0; i < endpoint.getConcurrentConsumers(); i++) {
            executor.submit(new SubscriberWrapper());
        }
    }

    @Override
    protected void doStop() throws Exception {
        localLog.info("Stopping Google PubSub consumer for {}/{}", endpoint.getProjectId(), endpoint.getDestinationName());

        if (!subscribers.isEmpty()) {
            localLog.info("Stopping subscribers for {}/{}", endpoint.getProjectId(), endpoint.getDestinationName());
            subscribers.forEach(AbstractApiService::stopAsync);
        }

        safeCancelSynchronousPullResponses();

        if (executor != null) {
            if (getEndpoint() != null && getEndpoint().getCamelContext() != null) {
                getEndpoint().getCamelContext().getExecutorServiceManager().shutdownGraceful(executor);
            } else {
                executor.shutdownNow();
            }
        }
        executor = null;

        super.doStop();
    }

    private void safeCancelSynchronousPullResponses() {
        for (ApiFuture<PullResponse> pullResponseApiFuture : pendingSynchronousPullResponses) {
            try {
                pullResponseApiFuture.cancel(true);
            } catch (Exception e) {
                localLog.warn("Exception while cancelling pending synchronous pull response", e);
            }
        }
        pendingSynchronousPullResponses.clear();
    }

    private int resolveMaxDeliveryAttempts() {
        if (endpoint.isMaxDeliveryAttemptsExplicitlySet()) {
            localLog.debug("Using explicitly configured maxDeliveryAttempts: {}", endpoint.getMaxDeliveryAttempts());
            return endpoint.getMaxDeliveryAttempts();
        }

        String subscriptionName = ProjectSubscriptionName.format(endpoint.getProjectId(), endpoint.getDestinationName());
        try (SubscriptionAdminClient adminClient = endpoint.getComponent().getSubscriptionAdminClient(endpoint)) {
            Subscription subscription = adminClient.getSubscription(subscriptionName);
            if (subscription.hasDeadLetterPolicy()) {
                DeadLetterPolicy dlp = subscription.getDeadLetterPolicy();
                int maxAttempts = dlp.getMaxDeliveryAttempts();
                if (maxAttempts > 0) {
                    localLog.info("Auto-fetched maxDeliveryAttempts={} from subscription dead-letter policy for {}",
                            maxAttempts, endpoint.getDestinationName());
                    return maxAttempts;
                }
            }
            localLog.debug("No dead-letter policy found on subscription {}, maxDeliveryAttempts enforcement disabled",
                    endpoint.getDestinationName());
        } catch (Exception e) {
            localLog.warn("Failed to auto-fetch maxDeliveryAttempts from subscription {}: {}. "
                          + "Max delivery attempts enforcement will be disabled. "
                          + "Set the maxDeliveryAttempts endpoint option explicitly to enable enforcement.",
                    endpoint.getDestinationName(), e.getMessage());
            localLog.debug("Auto-fetch failure details", e);
        }
        return 0;
    }

    public int getResolvedMaxDeliveryAttempts() {
        return resolvedMaxDeliveryAttempts;
    }

    private class SubscriberWrapper implements Runnable {

        private final String subscriptionName;

        SubscriberWrapper() {
            subscriptionName = ProjectSubscriptionName.format(endpoint.getProjectId(), endpoint.getDestinationName());
        }

        @Override
        public void run() {
            try {
                if (localLog.isDebugEnabled()) {
                    localLog.debug("Subscribing to {}", subscriptionName);
                }

                if (endpoint.isSynchronousPull()) {
                    synchronousPull(subscriptionName);
                } else {
                    asynchronousPull(subscriptionName);
                }

                localLog.debug("Exit run for subscription {}", subscriptionName);
            } catch (Exception e) {
                if (e instanceof InterruptedException) {
                    Thread.currentThread().interrupt();
                }
                localLog.error("Failure getting messages from PubSub", e);

                // allow camel error handler to be aware
                if (endpoint.isBridgeErrorHandler()) {
                    getExceptionHandler().handleException(e);
                }
            }
        }

        private void asynchronousPull(String subscriptionName) throws IOException {
            while (isRunAllowed() && !isSuspendingOrSuspended()) {
                MessageReceiver messageReceiver = new CamelMessageReceiver(GooglePubsubConsumer.this, endpoint, processor);

                Subscriber subscriber = endpoint.getComponent().getSubscriber(subscriptionName, messageReceiver, endpoint);
                boolean subscriberAdded = false;
                try {
                    subscriber.startAsync().awaitRunning();
                    // Only add to list after successful startup
                    subscribers.add(subscriber);
                    subscriberAdded = true;
                    subscriber.awaitTerminated();
                } catch (Exception e) {
                    // Remove from list if it was added
                    if (subscriberAdded) {
                        subscribers.remove(subscriber);
                    }

                    // Check if error is recoverable
                    boolean isRecoverable = false;
                    if (e instanceof ApiException ae) {
                        isRecoverable = ae.isRetryable();
                    } else if (e.getCause() instanceof ApiException ae) {
                        isRecoverable = ae.isRetryable();
                    }

                    if (isRecoverable) {
                        localLog.error("Retryable error getting messages from PubSub", e);
                    } else {
                        localLog.error("Non-recoverable error getting messages from PubSub, stopping subscriber loop", e);
                    }

                    // allow camel error handler to be aware
                    if (endpoint.isBridgeErrorHandler()) {
                        getExceptionHandler().handleException(e);
                    }

                    // For non-recoverable errors, exit the loop
                    if (!isRecoverable) {
                        break;
                    }

                    // Add backoff delay for recoverable errors to prevent tight loop
                    // We use initialDelay for the actual delay, and maxIterations(1) to run once
                    Tasks.foregroundTask()
                            .withBudget(Budgets.iterationBudget()
                                    .withMaxIterations(1)
                                    .withInitialDelay(Duration.ofSeconds(5))
                                    .withInterval(Duration.ZERO)
                                    .build())
                            .withName("PubSubRetryDelay")
                            .build()
                            .run(getEndpoint().getCamelContext(), () -> true);
                } finally {
                    localLog.debug("Stopping async subscriber {}", subscriptionName);
                    subscriber.stopAsync();
                    // Ensure cleanup from list
                    if (subscriberAdded) {
                        subscribers.remove(subscriber);
                    }
                }
            }
        }

        private void synchronousPull(String subscriptionName) throws ExecutionException, InterruptedException {
            while (isRunAllowed() && !isSuspendingOrSuspended()) {
                ApiFuture<PullResponse> synchronousPullResponseFuture = null;
                try (SubscriberStub subscriber = endpoint.getComponent().getSubscriberStub(endpoint)) {

                    PullRequest pullRequest = PullRequest.newBuilder()
                            .setMaxMessages(endpoint.getMaxMessagesPerPoll())
                            .setReturnImmediately(false)
                            .setSubscription(subscriptionName)
                            .build();

                    synchronousPullResponseFuture = subscriber.pullCallable().futureCall(pullRequest);
                    pendingSynchronousPullResponses.add(synchronousPullResponseFuture);
                    PullResponse pullResponse = synchronousPullResponseFuture.get();
                    for (ReceivedMessage message : pullResponse.getReceivedMessagesList()) {
                        PubsubMessage pubsubMessage = message.getMessage();
                        Exchange exchange = createExchange(true);
                        exchange.getIn().setBody(pubsubMessage.getData().toByteArray());

                        // Standard headers
                        exchange.getIn().setHeader(GooglePubsubConstants.ACK_ID, message.getAckId());
                        exchange.getIn().setHeader(GooglePubsubConstants.MESSAGE_ID, pubsubMessage.getMessageId());
                        exchange.getIn().setHeader(GooglePubsubConstants.PUBLISH_TIME, pubsubMessage.getPublishTime());
                        // Deprecated:  replaced by headerFilterStrategy
                        exchange.getIn().setHeader(GooglePubsubConstants.ATTRIBUTES, pubsubMessage.getAttributesMap());

                        // Delivery attempt (requires dead-letter policy on subscription)
                        int deliveryAttempt = message.getDeliveryAttempt();
                        if (deliveryAttempt > 0) {
                            exchange.getIn().setHeader(GooglePubsubConstants.DELIVERY_ATTEMPT, deliveryAttempt);
                        }

                        // Enforce maxDeliveryAttempts: nack without processing if limit reached
                        if (resolvedMaxDeliveryAttempts > 0 && deliveryAttempt >= resolvedMaxDeliveryAttempts) {
                            localLog.info(
                                    "Message {} has reached max delivery attempts ({}/{}), nacking to route to dead-letter topic",
                                    pubsubMessage.getMessageId(), deliveryAttempt, resolvedMaxDeliveryAttempts);
                            GooglePubsubAcknowledge ack = new AcknowledgeSync(
                                    () -> endpoint.getComponent().getSubscriberStub(endpoint), subscriptionName);
                            ack.nack(exchange);
                            continue;
                        }

                        //existing subscriber can not be propagated, because it will be closed at the end of this block
                        //subscriber will be created at the moment of use
                        // (see  https://issues.apache.org/jira/browse/CAMEL-18447)
                        GooglePubsubAcknowledge acknowledge = new AcknowledgeSync(
                                () -> endpoint.getComponent().getSubscriberStub(endpoint), subscriptionName);
                        if (endpoint.getAckMode() != GooglePubsubConstants.AckMode.NONE) {
                            exchange.getExchangeExtension().addOnCompletion(new AcknowledgeCompletion(acknowledge));
                        } else {
                            exchange.getIn().setHeader(GooglePubsubConstants.GOOGLE_PUBSUB_ACKNOWLEDGE, acknowledge);
                        }

                        // Inherit the rest of headers
                        for (String pubSubHeader : pubsubMessage.getAttributesMap().keySet()) {
                            String value = pubsubMessage.getAttributesMap().get(pubSubHeader);
                            if (headerFilterStrategy != null
                                    && headerFilterStrategy.applyFilterToExternalHeaders(pubSubHeader, value, exchange)) {
                                continue;
                            }
                            exchange.getIn().setHeader(pubSubHeader, value);
                        }

                        try {
                            processor.process(exchange);
                        } catch (Exception e) {
                            exchange.setException(e);
                        }

                        // Handle exception if one occurred
                        if (exchange.getException() != null) {
                            getExceptionHandler().handleException("Error processing exchange", exchange,
                                    exchange.getException());
                        }

                        // Execute synchronization callbacks (ACK/NACK) based on exchange status
                        // This is required because we are directly calling processor.process() outside of the normal
                        // Camel routing engine, so we must manually trigger the OnCompletion callbacks
                        if (endpoint.getAckMode() != GooglePubsubConstants.AckMode.NONE) {
                            List<Synchronization> synchronizations = exchange.getExchangeExtension().handoverCompletions();
                            UnitOfWorkHelper.doneSynchronizations(exchange, synchronizations);
                        }
                    }
                } catch (CancellationException e) {
                    localLog.debug("PubSub synchronous pull request cancelled", e);
                } catch (IOException e) {
                    localLog.error("I/O exception while getting messages from PubSub. Reconnecting.", e);
                } catch (ExecutionException e) {
                    if (e.getCause() instanceof ApiException && ((ApiException) (e.getCause())).isRetryable()) {
                        localLog.error("Retryable API exception in getting messages from PubSub", e.getCause());
                    } else {
                        throw e;
                    }
                } finally {
                    if (synchronousPullResponseFuture != null) {
                        pendingSynchronousPullResponses.remove(synchronousPullResponseFuture);
                    }
                }
            }
        }
    }
}
