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

import static org.apache.iceberg.types.Types.NestedField.optional;
import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
import org.apache.iceberg.types.Types;
import org.junit.jupiter.api.Test;

public class TestTrackedFile {

  private static final Schema TABLE_SCHEMA =
      new Schema(
          optional(1, "id", Types.IntegerType.get()), optional(2, "data", Types.StringType.get()));

  private static final Types.StructType CONTENT_STATS_TYPE =
      StatsUtil.statsReadSchema(TABLE_SCHEMA, ImmutableList.of(1, 2));

  private static final Types.StructType PARTITION_TYPE =
      PartitionSpec.builderFor(TABLE_SCHEMA).identity("id").build().partitionType();

  @Test
  public void schemaFieldOrder() {
    Types.StructType type = TrackedFile.schema(PARTITION_TYPE, CONTENT_STATS_TYPE).asStruct();
    List<Types.NestedField> fields = type.fields();

    assertThat(fields)
        .extracting(Types.NestedField::name)
        .containsExactly(
            "tracking",
            "content_type",
            "format_version",
            "location",
            "file_format",
            "record_count",
            "file_size_in_bytes",
            "spec_id",
            "partition",
            "content_stats",
            "sort_order_id",
            "deletion_vector",
            "manifest_info",
            "key_metadata",
            "split_offsets",
            "equality_ids");
  }

  @Test
  public void schemaFieldIds() {
    Types.StructType type = TrackedFile.schema(PARTITION_TYPE, CONTENT_STATS_TYPE).asStruct();
    List<Types.NestedField> fields = type.fields();

    assertThat(fields)
        .extracting(Types.NestedField::fieldId)
        .containsExactly(
            147, 134, 157, 100, 101, 103, 104, 141, 102, 146, 140, 148, 150, 131, 132, 135);
  }

  @Test
  public void schemaUsesProvidedType() {
    Types.StructType type = TrackedFile.schema(PARTITION_TYPE, CONTENT_STATS_TYPE).asStruct();
    Types.NestedField contentStatsField = type.field(TrackedFile.CONTENT_STATS_ID);
    Types.NestedField partitionField = type.field(TrackedFile.PARTITION_ID);

    assertThat(contentStatsField.type().asStructType()).isEqualTo(CONTENT_STATS_TYPE);
    assertThat(partitionField.type().asStructType()).isEqualTo(PARTITION_TYPE);
  }

  @Test
  public void schemaReflectsInput() {
    Schema smallSchema = new Schema(optional(1, "id", Types.IntegerType.get()));
    Schema largeSchema =
        new Schema(
            optional(1, "id", Types.IntegerType.get()),
            optional(2, "data", Types.StringType.get()),
            optional(3, "ts", Types.TimestampType.withoutZone()));

    Types.StructType smallStats = StatsUtil.statsReadSchema(smallSchema, ImmutableList.of(1));
    Types.StructType largeStats = StatsUtil.statsReadSchema(largeSchema, ImmutableList.of(1, 3));

    Types.StructType smallType = TrackedFile.schema(PARTITION_TYPE, smallStats).asStruct();
    Types.StructType largeType = TrackedFile.schema(PARTITION_TYPE, largeStats).asStruct();

    Types.StructType smallResult =
        smallType.field(TrackedFile.CONTENT_STATS_ID).type().asStructType();
    Types.StructType largeResult =
        largeType.field(TrackedFile.CONTENT_STATS_ID).type().asStructType();

    assertThat(smallResult.fields()).hasSize(1);
    assertThat(largeResult.fields()).hasSize(2);
  }

  @Test
  public void schemaPartitionIsOptional() {
    Types.StructType type = TrackedFile.schema(PARTITION_TYPE, CONTENT_STATS_TYPE).asStruct();
    Types.NestedField partitionField = type.field(TrackedFile.PARTITION_ID);

    assertThat(partitionField.isOptional()).isTrue();
    assertThat(partitionField.name()).isEqualTo(TrackedFile.PARTITION_NAME);
    assertThat(partitionField.doc()).isEqualTo(TrackedFile.PARTITION_DOC);
  }

  @Test
  public void schemaUsesUnknownForEmptyStructs() {
    Types.StructType type =
        TrackedFile.schema(Types.StructType.of(), Types.StructType.of()).asStruct();

    assertThat(type.field(TrackedFile.PARTITION_ID).type()).isEqualTo(Types.UnknownType.get());
    assertThat(type.field(TrackedFile.CONTENT_STATS_ID).type()).isEqualTo(Types.UnknownType.get());

    Types.StructType partitionedType =
        TrackedFile.schema(PARTITION_TYPE, Types.StructType.of()).asStruct();
    assertThat(partitionedType.field(TrackedFile.PARTITION_ID).type()).isEqualTo(PARTITION_TYPE);
    assertThat(partitionedType.field(TrackedFile.CONTENT_STATS_ID).type())
        .isEqualTo(Types.UnknownType.get());
  }
}
