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

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.parquet.HadoopReadOptions;
import org.apache.parquet.ParquetReadOptions;
import org.apache.parquet.example.data.Group;
import org.apache.parquet.example.data.simple.SimpleGroupFactory;
import org.apache.parquet.hadoop.example.ExampleParquetWriter;
import org.apache.parquet.hadoop.example.GroupWriteSupport;
import org.apache.parquet.hadoop.metadata.ParquetMetadata;
import org.apache.parquet.hadoop.util.HadoopInputFile;
import org.apache.parquet.hadoop.util.HadoopOutputFile;
import org.apache.parquet.schema.MessageType;
import org.apache.parquet.schema.MessageTypeParser;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class TestParquetFileReaderMaxMessageSize {

  public static Path TEST_FILE;
  public MessageType schema;

  @TempDir
  private java.nio.file.Path tempDir;

  @BeforeEach
  public void testSetup() throws IOException {

    TEST_FILE = new Path(tempDir.resolve("many-columns.parquet").toUri());
    // Create a file with many columns
    StringBuilder schemaBuilder = new StringBuilder("message test_schema {");
    for (int i = 0; i < 2000; i++) {
      schemaBuilder.append("required int64 col_").append(i).append(";");
    }
    schemaBuilder.append("}");

    schema = MessageTypeParser.parseMessageType(schemaBuilder.toString());

    Configuration conf = new Configuration();
    GroupWriteSupport.setSchema(schema, conf);

    try (ParquetWriter<Group> writer = ExampleParquetWriter.builder(HadoopOutputFile.fromPath(TEST_FILE, conf))
        .withConf(conf)
        .withType(schema)
        .build()) {

      SimpleGroupFactory factory = new SimpleGroupFactory(schema);
      Group group = factory.newGroup();
      for (int col = 0; col < 2000; col++) {
        group.append("col_" + col, 1L);
      }
      writer.write(group);
    }
  }

  /**
   * Test reading a file with many columns using custom max message size
   */
  @Test
  public void testReadFileWithManyColumns() throws IOException {
    Configuration readConf = new Configuration();
    readConf.setInt("parquet.thrift.string.size.limit", 200 * 1024 * 1024);

    ParquetReadOptions options = HadoopReadOptions.builder(readConf).build();

    try (ParquetFileReader reader =
        ParquetFileReader.open(HadoopInputFile.fromPath(TEST_FILE, readConf), options)) {

      ParquetMetadata metadata = reader.getFooter();
      assertThat(metadata).isNotNull();
      assertThat(metadata.getFileMetaData().getSchema()).isEqualTo(schema);
      assertThat(metadata.getBlocks()).isNotEmpty();
    }
  }

  /**
   * Test that default configuration works for normal files
   */
  @Test
  public void testReadNormalFileWithDefaultConfig() throws IOException {
    // Read with default configuration (no custom max message size)
    Configuration readConf = new Configuration();
    ParquetReadOptions options = HadoopReadOptions.builder(readConf).build();

    try (ParquetFileReader reader =
        ParquetFileReader.open(HadoopInputFile.fromPath(TEST_FILE, readConf), options)) {

      ParquetMetadata metadata = reader.getFooter();
      assertThat(metadata).isNotNull();
      assertThat(metadata.getBlocks().get(0).getRowCount()).isEqualTo(1);
    }
  }

  /**
   * Test that insufficient max message size produces error
   */
  @Test
  public void testInsufficientMaxMessageSizeError() {
    // Try to read with very small max message size
    Configuration readConf = new Configuration();
    readConf.setInt("parquet.thrift.string.size.limit", 1); // Only 1 byte

    ParquetReadOptions options = HadoopReadOptions.builder(readConf).build();

    assertThatThrownBy(() -> {
          try (ParquetFileReader reader =
              ParquetFileReader.open(HadoopInputFile.fromPath(TEST_FILE, readConf), options)) {
            reader.getFooter();
          }
        })
        .isInstanceOf(IOException.class)
        .hasMessageMatching("(?s).*(Message size exceeds limit|MaxMessageSize reached).*");
  }

  /**
   * The -1 sentinel must be honored as "use the default max message size",
   * not rejected as a non-positive value.
   */
  @Test
  public void testReadAcceptsMinusOneAsDefaultMaxMessageSize() throws IOException {
    Configuration readConf = new Configuration();
    readConf.setInt("parquet.thrift.string.size.limit", -1);
    ParquetReadOptions options = HadoopReadOptions.builder(readConf).build();

    try (ParquetFileReader reader =
        ParquetFileReader.open(HadoopInputFile.fromPath(TEST_FILE, readConf), options)) {
      ParquetMetadata metadata = reader.getFooter();
      assertThat(metadata).isNotNull();
      assertThat(metadata.getFileMetaData().getSchema()).isEqualTo(schema);
    }
  }

  @Test
  public void testReadRejectsZeroMaxMessageSize() throws IOException {
    assertRejectsNonPositiveMaxMessageSize(0);
  }

  @Test
  public void testReadRejectsInvalidNegativeMaxMessageSize() throws IOException {
    assertRejectsNonPositiveMaxMessageSize(-5);
  }

  private void assertRejectsNonPositiveMaxMessageSize(int maxMessageSize) {
    Configuration readConf = new Configuration();
    readConf.setInt("parquet.thrift.string.size.limit", maxMessageSize);
    ParquetReadOptions options = HadoopReadOptions.builder(readConf).build();

    assertThatThrownBy(() -> {
          try (ParquetFileReader reader =
              ParquetFileReader.open(HadoopInputFile.fromPath(TEST_FILE, readConf), options)) {
            reader.getFooter();
          }
        })
        .isInstanceOf(NumberFormatException.class)
        .hasMessage("Max message size must be positive: " + maxMessageSize);
  }
}
