/*
 * Licensed 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 io.trino.plugin.iceberg;

import io.trino.spi.type.DecimalType;
import io.trino.spi.type.Decimals;
import io.trino.spi.type.TimeZoneKey;
import io.trino.spi.type.UuidType;
import org.apache.iceberg.types.Type;
import org.apache.iceberg.types.Types;

import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.UUID;

import static io.airlift.slice.Slices.utf8Slice;
import static io.trino.plugin.iceberg.util.Timestamps.timestampTzFromMicros;
import static io.trino.spi.type.Decimals.isShortDecimal;
import static io.trino.spi.type.Timestamps.PICOSECONDS_PER_MICROSECOND;

public final class IcebergTypes
{
    private IcebergTypes() {}

    /**
     * Convert value from Iceberg representation to Trino representation.
     */
    public static Object convertIcebergValueToTrino(Type icebergType, Object value)
    {
        if (value == null) {
            return null;
        }
        if (icebergType instanceof Types.IntegerType) {
            return ((Integer) value).longValue();
        }
        if (icebergType instanceof Types.FloatType) {
            return (long) Float.floatToIntBits((Float) value);
        }
        if (icebergType instanceof Types.DecimalType) {
            Types.DecimalType icebergDecimalType = (Types.DecimalType) icebergType;
            DecimalType trinoDecimalType = DecimalType.createDecimalType(icebergDecimalType.precision(), icebergDecimalType.scale());
            if (isShortDecimal(trinoDecimalType)) {
                return Decimals.encodeShortScaledValue((BigDecimal) value, trinoDecimalType.getScale());
            }
            return Decimals.encodeScaledValue((BigDecimal) value, trinoDecimalType.getScale());
        }
        if (icebergType instanceof Types.StringType) {
            // Partition values are passed as String, but min/max values are passed as a CharBuffer
            if (value instanceof CharBuffer) {
                value = new String(((CharBuffer) value).array());
            }
            return utf8Slice(((String) value));
        }
        if (icebergType instanceof Types.BinaryType) {
            // TODO the client sees the bytearray's tostring ouput instead of seeing actual bytes, needs to be fixed.
            return ((ByteBuffer) value).array().clone();
        }
        if (icebergType instanceof Types.DateType) {
            return ((Integer) value).longValue();
        }
        if (icebergType instanceof Types.TimeType) {
            return Math.multiplyExact((Long) value, PICOSECONDS_PER_MICROSECOND);
        }
        if (icebergType instanceof Types.TimestampType) {
            long epochMicros = (long) value;
            if (((Types.TimestampType) icebergType).shouldAdjustToUTC()) {
                return timestampTzFromMicros(epochMicros, TimeZoneKey.UTC_KEY);
            }
            return epochMicros;
        }
        if (icebergType instanceof Types.UUIDType) {
            return UuidType.javaUuidToTrinoUuid((UUID) value);
        }

        // TODO implement explicit conversion for all supported types
        return value;
    }
}

@@ -1532,198 +1532,619 @@
 pes.
-IntegerType) %7B%0A            return ((Integer) value).longValue();%0A        %7D%0A        if (icebergType instanceof Types.FloatType) %7B%0A            return (long) Float.floatToIntBits((Float) value)
+BooleanType) %7B%0A            //noinspection RedundantCast%0A            return (Boolean) value;%0A        %7D%0A        if (icebergType instanceof Types.IntegerType) %7B%0A            return ((Integer) value).longValue();%0A        %7D%0A        if (icebergType instanceof Types.LongType) %7B%0A            //noinspection RedundantCast%0A            return (Long) value;%0A        %7D%0A        if (icebergType instanceof Types.FloatType) %7B%0A            return (long) Float.floatToIntBits((Float) value);%0A        %7D%0A        if (icebergType instanceof Types.DoubleType) %7B%0A            //noinspection RedundantCast%0A            return (Double) value
 ;%0A  
@@ -3215,16 +3215,49 @@
  fixed.%0A
+            // TODO return Slice%0A
         
@@ -4026,90 +4026,91 @@
     
-// TODO implement explicit conversion for all supported types%0A        return value
+throw new UnsupportedOperationException(%22Unsupported iceberg type: %22 + icebergType)
 ;%0A  

Convert from Iceberg to Trino representation strictly