/*
 * QQQ - Low-code Application Framework for Engineers.
 * Copyright (C) 2021-2022.  Kingsrook, LLC
 * 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
 * contact@kingsrook.com
 * https://github.com/Kingsrook/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package com.kingsrook.qqq.backend.core.actions.scripts;


import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import com.kingsrook.qqq.backend.core.actions.scripts.logging.Log4jCodeExecutionLogger;
import com.kingsrook.qqq.backend.core.actions.scripts.logging.QCodeExecutionLoggerInterface;
import com.kingsrook.qqq.backend.core.actions.scripts.logging.ScriptExecutionLoggerInterface;
import com.kingsrook.qqq.backend.core.actions.scripts.logging.StoreScriptLogAndScriptLogLineExecutionLogger;
import com.kingsrook.qqq.backend.core.exceptions.QCodeException;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.logging.QLogger;
import com.kingsrook.qqq.backend.core.model.actions.scripts.AbstractRunScriptInput;
import com.kingsrook.qqq.backend.core.model.actions.scripts.ExecuteCodeInput;
import com.kingsrook.qqq.backend.core.model.actions.scripts.ExecuteCodeOutput;
import com.kingsrook.qqq.backend.core.model.metadata.code.QCodeReference;
import com.kingsrook.qqq.backend.core.model.metadata.code.QCodeType;
import com.kingsrook.qqq.backend.core.model.scripts.ScriptRevision;
import com.kingsrook.qqq.backend.core.utils.StringUtils;


/*******************************************************************************
 ** Action to execute user/runtime defined code.
 **
 ** This action is designed to support code in multiple languages, by using
 ** executors, e.g., provided by additional runtime qqq dependencies.  Initially
 ** we are building qqq-language-support-javascript.
 **
 ** We also have a Java executor, to provide at least a little bit of testability
 ** within qqq-backend-core.  This executor is a candidate to be replaced in the
 ** future with something that would do actual dynamic java (whether that's compiled
 ** at runtime, or loaded from a plugin jar at runtime).  In other words, the java
 ** executor in place today is just meant to be a placeholder.
 *******************************************************************************/
public class ExecuteCodeAction
{
   private static final QLogger LOG = QLogger.getLogger(ExecuteCodeAction.class);



   /*******************************************************************************
    **
    *******************************************************************************/
   @SuppressWarnings("checkstyle:indentation")
   public void run(ExecuteCodeInput input, ExecuteCodeOutput output) throws QException, QCodeException
   {
      QCodeReference codeReference = input.getCodeReference();

      QCodeExecutionLoggerInterface executionLogger = input.getExecutionLogger();
      if(executionLogger == null)
      {
         executionLogger = getDefaultExecutionLogger();
      }
      executionLogger.acceptExecutionStart(input);

      try
      {
         String languageExecutor = switch(codeReference.getCodeType())
         {
            case JAVA -> "com.kingsrook.qqq.backend.core.actions.scripts.QJavaExecutor";
            case JAVA_SCRIPT -> "com.kingsrook.qqq.languages.javascript.QJavaScriptExecutor";
         };

         @SuppressWarnings("unchecked")
         Class<? extends QCodeExecutor> executorClass = (Class<? extends QCodeExecutor>) Class.forName(languageExecutor);
         QCodeExecutor qCodeExecutor = executorClass.getConstructor().newInstance();

         ////////////////////////////////////////////////////////////////////////////////////////////////////
         // merge all of the input context, plus the input... input - into a context for the code executor //
         ////////////////////////////////////////////////////////////////////////////////////////////////////
         Map<String, Serializable> context = new HashMap<>();
         if(input.getContext() != null)
         {
            context.putAll(input.getContext());
         }
         if(input.getInput() != null)
         {
            context.putAll(input.getInput());
         }

         Serializable codeOutput = qCodeExecutor.execute(codeReference, context, executionLogger);
         output.setOutput(codeOutput);
         executionLogger.acceptExecutionEnd(codeOutput);
      }
      catch(QCodeException qCodeException)
      {
         executionLogger.acceptException(qCodeException);
         throw (qCodeException);
      }
      catch(Exception e)
      {
         executionLogger.acceptException(e);
         throw (new QException("Error executing code [" + codeReference + "]", e));
      }
   }



   /*******************************************************************************
    **
    *******************************************************************************/
   public static ExecuteCodeInput setupExecuteCodeInput(AbstractRunScriptInput<?> input, ScriptRevision scriptRevision)
   {
      ExecuteCodeInput executeCodeInput = new ExecuteCodeInput();
      executeCodeInput.setInput(new HashMap<>(Objects.requireNonNullElseGet(input.getInputValues(), HashMap::new)));
      executeCodeInput.setContext(new HashMap<>());

      Map<String, Serializable> context = executeCodeInput.getContext();
      if(input.getOutputObject() != null)
      {
         context.put("output", input.getOutputObject());
      }

      if(input.getScriptUtils() != null)
      {
         context.put("scriptUtils", input.getScriptUtils());
      }

      executeCodeInput.setCodeReference(new QCodeReference().withInlineCode(scriptRevision.getContents()).withCodeType(QCodeType.JAVA_SCRIPT)); // todo - code type as attribute of script!!

      ExecuteCodeAction.addApiUtilityToContext(context, scriptRevision);
      context.put("qqq", new QqqScriptUtils());
      ExecuteCodeAction.setExecutionLoggerInExecuteCodeInput(input, scriptRevision, executeCodeInput);

      return (executeCodeInput);
   }



   /*******************************************************************************
    ** Try to (dynamically) load the ApiScriptUtils object from the api middleware
    ** module -- in case the runtime doesn't have that module deployed (e.g, not in
    ** the project pom).
    *******************************************************************************/
   public static void addApiUtilityToContext(Map<String, Serializable> context, ScriptRevision scriptRevision)
   {
      if(!StringUtils.hasContent(scriptRevision.getApiName()) || !StringUtils.hasContent(scriptRevision.getApiVersion()))
      {
         return;
      }

      try
      {
         Class<?> apiScriptUtilsClass  = Class.forName("com.kingsrook.qqq.api.utils.ApiScriptUtils");
         Object   apiScriptUtilsObject = apiScriptUtilsClass.getConstructor(String.class, String.class).newInstance(scriptRevision.getApiName(), scriptRevision.getApiVersion());
         context.put("api", (Serializable) apiScriptUtilsObject);
      }
      catch(ClassNotFoundException e)
      {
         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
         // this is the only exception we're kinda expecting here - so catch for it specifically, and just log.trace - others, warn //
         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
         LOG.trace("Couldn't load ApiScriptUtils class - qqq-middleware-api not on the classpath?");
      }
      catch(Exception e)
      {
         LOG.warn("Error adding api utility to script context", e);
      }
   }



   /*******************************************************************************
    **
    *******************************************************************************/
   private static void setExecutionLoggerInExecuteCodeInput(AbstractRunScriptInput<?> input, ScriptRevision scriptRevision, ExecuteCodeInput executeCodeInput)
   {
      /////////////////////////////////////////////////////////////////////////////////////////////////
      // let caller supply a logger, or by default use StoreScriptLogAndScriptLogLineExecutionLogger //
      /////////////////////////////////////////////////////////////////////////////////////////////////
      QCodeExecutionLoggerInterface executionLogger = Objects.requireNonNullElseGet(input.getLogger(), () -> new StoreScriptLogAndScriptLogLineExecutionLogger(scriptRevision.getScriptId(), scriptRevision.getId()));
      executeCodeInput.setExecutionLogger(executionLogger);
      if(executionLogger instanceof ScriptExecutionLoggerInterface scriptExecutionLoggerInterface)
      {
         ////////////////////////////////////////////////////////////////////////////////////////////////////
         // if logger is aware of scripts (as opposed to a generic CodeExecution logger), give it the ids. //
         ////////////////////////////////////////////////////////////////////////////////////////////////////
         scriptExecutionLoggerInterface.setScriptId(scriptRevision.getScriptId());
         scriptExecutionLoggerInterface.setScriptRevisionId(scriptRevision.getId());
      }
   }



   /*******************************************************************************
    **
    *******************************************************************************/
   private QCodeExecutionLoggerInterface getDefaultExecutionLogger()
   {
      return (new Log4jCodeExecutionLogger());
   }

}
