/*
 * 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.comet.serde

import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types._

import org.apache.comet.serde.QueryPlanSerde.{createBinaryExpr, exprToProtoInternal, optExprWithFallbackReason, scalarFunctionExprToProto}

object CometMapKeys extends CometExpressionSerde[MapKeys] {

  override def convert(
      expr: MapKeys,
      inputs: Seq[Attribute],
      binding: Boolean): Option[ExprOuterClass.Expr] = {
    val childExpr = exprToProtoInternal(expr.child, inputs, binding)
    val mapKeysScalarExpr = scalarFunctionExprToProto("map_keys", childExpr)
    optExprWithFallbackReason(mapKeysScalarExpr, expr, expr.children: _*)
  }
}

object CometMapEntries extends CometExpressionSerde[MapEntries] {

  override def convert(
      expr: MapEntries,
      inputs: Seq[Attribute],
      binding: Boolean): Option[ExprOuterClass.Expr] = {
    val childExpr = exprToProtoInternal(expr.child, inputs, binding)
    val mapEntriesScalarExpr = scalarFunctionExprToProto("map_entries", childExpr)
    optExprWithFallbackReason(mapEntriesScalarExpr, expr, expr.children: _*)
  }
}

object CometMapValues extends CometExpressionSerde[MapValues] {

  override def convert(
      expr: MapValues,
      inputs: Seq[Attribute],
      binding: Boolean): Option[ExprOuterClass.Expr] = {
    val childExpr = exprToProtoInternal(expr.child, inputs, binding)
    val mapValuesScalarExpr = scalarFunctionExprToProto("map_values", childExpr)
    optExprWithFallbackReason(mapValuesScalarExpr, expr, expr.children: _*)
  }
}

object CometMapExtract extends CometExpressionSerde[GetMapValue] {

  override def convert(
      expr: GetMapValue,
      inputs: Seq[Attribute],
      binding: Boolean): Option[ExprOuterClass.Expr] = {
    val mapExpr = exprToProtoInternal(expr.child, inputs, binding)
    val keyExpr = exprToProtoInternal(expr.key, inputs, binding)
    val mapExtractExpr = scalarFunctionExprToProto("map_extract", mapExpr, keyExpr)
    optExprWithFallbackReason(mapExtractExpr, expr, expr.children: _*)
  }
}

object CometMapFromArrays extends CometExpressionSerde[MapFromArrays] {

  override def convert(
      expr: MapFromArrays,
      inputs: Seq[Attribute],
      binding: Boolean): Option[ExprOuterClass.Expr] = {
    val keysExpr = exprToProtoInternal(expr.left, inputs, binding)
    val valuesExpr = exprToProtoInternal(expr.right, inputs, binding)
    val keyType = expr.left.dataType.asInstanceOf[ArrayType].elementType
    val valueType = expr.right.dataType.asInstanceOf[ArrayType].elementType
    val returnType = MapType(keyType = keyType, valueType = valueType)
    for {
      andBinaryExprProto <- createAndBinaryExpr(expr, inputs, binding)
      mapFromArraysExprProto <- scalarFunctionExprToProto("map", keysExpr, valuesExpr)
      nullLiteralExprProto <- exprToProtoInternal(Literal(null, returnType), inputs, binding)
    } yield {
      val caseWhenExprProto = ExprOuterClass.CaseWhen
        .newBuilder()
        .addWhen(andBinaryExprProto)
        .addThen(mapFromArraysExprProto)
        .setElseExpr(nullLiteralExprProto)
        .build()
      ExprOuterClass.Expr
        .newBuilder()
        .setCaseWhen(caseWhenExprProto)
        .build()
    }
  }

  private def createAndBinaryExpr(
      expr: MapFromArrays,
      inputs: Seq[Attribute],
      binding: Boolean): Option[ExprOuterClass.Expr] = {
    createBinaryExpr(
      expr,
      IsNotNull(expr.left),
      IsNotNull(expr.right),
      inputs,
      binding,
      (builder, binaryExpr) => builder.setAnd(binaryExpr))
  }
}

object CometMapContainsKey extends CometExpressionSerde[MapContainsKey] {

  override def convert(
      expr: MapContainsKey,
      inputs: Seq[Attribute],
      binding: Boolean): Option[ExprOuterClass.Expr] = {
    // Replace with array_has(map_keys(map), key)
    val mapExpr = exprToProtoInternal(expr.left, inputs, binding)
    val keyExpr = exprToProtoInternal(expr.right, inputs, binding)

    val mapKeysExpr = scalarFunctionExprToProto("map_keys", mapExpr)

    val mapContainsKeyExpr = scalarFunctionExprToProto("array_has", mapKeysExpr, keyExpr)
    optExprWithFallbackReason(mapContainsKeyExpr, expr, expr.children: _*)
  }
}

object CometMapFromEntries
    extends CometScalarFunction[MapFromEntries]("map_from_entries")
    with CodegenDispatchFallback {
  val keyUnsupportedReason =
    "`BinaryType` is not supported as a map key in `map_from_entries`"
  val valueUnsupportedReason =
    "`BinaryType` is not supported as a map value in `map_from_entries`"

  override def getIncompatibleReasons(): Seq[String] =
    Seq(keyUnsupportedReason, valueUnsupportedReason)

  override def getSupportLevel(expr: MapFromEntries): SupportLevel = {
    if (SupportLevel.containsType(expr.dataType.keyType, classOf[BinaryType])) {
      return Incompatible(Some(keyUnsupportedReason))
    }
    if (SupportLevel.containsType(expr.dataType.valueType, classOf[BinaryType])) {
      return Incompatible(Some(valueUnsupportedReason))
    }
    Compatible(None)
  }
}

object CometStrToMap extends CometScalarFunction[StringToMap]("str_to_map") {

  // Spark 4.1.1+ honours spark.sql.legacy.truncateForEmptyRegexSplit by truncating trailing
  // empty entries from the split result. Comet's native str_to_map always behaves as if the flag
  // were false, so it is incompatible when legacy truncation is enabled. Read by string key so it
  // resolves on older Spark versions where the config is not registered.
  private val legacyTruncateConfig = "spark.sql.legacy.truncateForEmptyRegexSplit"

  override def getSupportLevel(expr: StringToMap): SupportLevel = {
    if (SQLConf.get.getConfString(legacyTruncateConfig, "false").toBoolean) {
      Incompatible(Some(s"$legacyTruncateConfig is enabled"))
    } else {
      Compatible(None)
    }
  }
}

object CometCreateMap extends CometCodegenDispatch[CreateMap]

object CometMapFilter extends CometCodegenDispatch[MapFilter]

object CometTransformKeys extends CometCodegenDispatch[TransformKeys]

object CometTransformValues extends CometCodegenDispatch[TransformValues]

object CometMapZipWith extends CometCodegenDispatch[MapZipWith]

object CometMapConcat extends CometCodegenDispatch[MapConcat]
