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

import org.apache.paimon.data.Timestamp;

import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;

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

/** Test for {@link DateTimeUtils}. */
public class DateTimeUtilsTest {

    @Test
    public void testFormatLocalDateTime() {
        LocalDateTime time = LocalDateTime.of(2023, 8, 30, 12, 30, 59, 999_999_999);
        String[] expectations = new String[10];
        expectations[0] = "2023-08-30 12:30:59";
        expectations[1] = "2023-08-30 12:30:59.9";
        for (int i = 2; i <= 9; i++) {
            expectations[i] = expectations[i - 1] + "9";
        }

        for (int precision = 0; precision <= 9; precision++) {
            assertThat(DateTimeUtils.formatLocalDateTime(time, precision))
                    .isEqualTo(expectations[precision]);
        }
    }

    @Test
    public void testParseTimestampData() {
        String dt = "2024-01-14 19:35:00.012";
        Timestamp ts = DateTimeUtils.parseTimestampData(dt, 3);
        assertThat(dt)
                .isEqualTo(
                        ts.toLocalDateTime()
                                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));

        dt = "2024-01-14 19:35:20";
        ts = DateTimeUtils.parseTimestampData(dt, 3);
        assertThat(dt)
                .isEqualTo(
                        ts.toLocalDateTime()
                                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        dt = "2024-01-14";
        ts = DateTimeUtils.parseTimestampData(dt, 3);
        assertThat(dt)
                .isEqualTo(ts.toLocalDateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
    }

    @Test
    public void testTimestamp() {
        int nanos = 100;
        java.sql.Timestamp timestamp = new java.sql.Timestamp(System.currentTimeMillis());
        for (int i = 0; i < 2000; i++) {
            timestamp = new java.sql.Timestamp(timestamp.getTime() + 60 * 1000);
            timestamp.setNanos(nanos + timestamp.getNanos());

            Timestamp t1 = Timestamp.fromSQLTimestamp(timestamp);
            Timestamp t2 = DateTimeUtils.toInternal(timestamp.getTime(), nanos);
            assertThat(t1).isEqualTo(t2);
        }
    }

    @Test
    public void testToInternalWithChangedTimeZone() {
        TimeZone timeZone = TimeZone.getDefault();
        try {
            TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
            java.sql.Timestamp ts = java.sql.Timestamp.valueOf("2024-06-01 00:00:00");
            Timestamp ts1 = DateTimeUtils.toInternal(ts.getTime(), ts.getNanos());
            assertThat(ts1.toString()).isEqualTo("2024-06-01T00:00");

            TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
            Timestamp ts2 = DateTimeUtils.toInternal(ts.getTime(), ts.getNanos());
            assertThat(ts2.toString()).isEqualTo("2024-06-01T08:00");
        } finally {
            TimeZone.setDefault(timeZone);
        }
    }

    @Test
    public void testTruncateDropsSubPrecisionFraction() {
        // A fractional second below 0.1s has leading zeros in its nano representation.
        // The narrowing cast to precision 3 must drop all nanoseconds below the millisecond.
        Timestamp nineNanos = Timestamp.fromLocalDateTime(LocalDateTime.of(1970, 1, 1, 0, 0, 0, 9));
        assertThat(nineNanos.toLocalDateTime().getNano()).isEqualTo(9);
        assertThat(DateTimeUtils.truncate(nineNanos, 3).toLocalDateTime().getNano()).isEqualTo(0);

        // Truncating .000123456 to precision 6 must keep exactly 6 fractional digits (.000123).
        Timestamp micros =
                Timestamp.fromLocalDateTime(LocalDateTime.of(1970, 1, 1, 0, 0, 0, 123_456));
        assertThat(micros.toLocalDateTime().getNano()).isEqualTo(123_456);
        assertThat(DateTimeUtils.truncate(micros, 6).toLocalDateTime().getNano())
                .isEqualTo(123_000);
    }

    @Test
    public void testTruncateIsIdempotent() {
        // An already-truncated value stays unchanged when truncated again to the same precision.
        Timestamp truncated =
                Timestamp.fromLocalDateTime(LocalDateTime.of(1970, 1, 1, 0, 0, 0, 123_000));
        assertThat(DateTimeUtils.truncate(truncated, 6).toLocalDateTime().getNano())
                .isEqualTo(123_000);
    }

    @Test
    public void testTruncateNoOpAtMaxPrecision() {
        // Precision 9 preserves all nanoseconds.
        Timestamp full =
                Timestamp.fromLocalDateTime(LocalDateTime.of(1970, 1, 1, 0, 0, 0, 123_456_789));
        assertThat(DateTimeUtils.truncate(full, 9).toLocalDateTime().getNano())
                .isEqualTo(123_456_789);
    }
}
