/*
 * 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.flink.kubernetes.operator.api.utils;

import org.apache.flink.api.common.JobStatus;
import org.apache.flink.kubernetes.operator.api.status.FlinkDeploymentStatus;
import org.apache.flink.kubernetes.operator.api.status.JobManagerDeploymentStatus;

import io.fabric8.kubernetes.api.model.Condition;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

/** Test for {@link ConditionsUtils}. */
public class ConditionsUtilsTest {

    private static Stream<Arguments> updateLastTransitionTimeParams() {
        String existingConditionLastTransitionTime = Instant.now().toString();

        return Stream.of(
                // Test Empty Conditions
                Arguments.of(
                        Collections.emptyList(),
                        new Condition(
                                "lastTransitionTime", "message", 0L, "reason", "status", "type"),
                        new Condition(
                                "lastTransitionTime", "message", 0L, "reason", "status", "type"),
                        false),

                // Test Correct Update
                Arguments.of(
                        List.of(
                                new Condition(
                                        existingConditionLastTransitionTime,
                                        "message",
                                        0L,
                                        "reason",
                                        "status",
                                        "type")),
                        new Condition(
                                existingConditionLastTransitionTime,
                                "message",
                                0L,
                                "reason",
                                "status",
                                "type"),
                        new Condition(
                                "lastTransitionTime", "message", 0L, "reason", "status", "type"),
                        true),

                // Test Status Unchanged
                Arguments.of(
                        List.of(
                                new Condition(
                                        existingConditionLastTransitionTime,
                                        "message",
                                        0L,
                                        "reason",
                                        "status",
                                        "type")),
                        new Condition(
                                existingConditionLastTransitionTime,
                                "message",
                                0L,
                                "reason",
                                "status",
                                "type"),
                        new Condition(
                                existingConditionLastTransitionTime,
                                "message",
                                0L,
                                "reason",
                                "status",
                                "type"),
                        true),

                // Test Status Changed
                Arguments.of(
                        List.of(
                                new Condition(
                                        existingConditionLastTransitionTime,
                                        "message",
                                        0L,
                                        "reason",
                                        "status1",
                                        "type")),
                        new Condition(
                                existingConditionLastTransitionTime,
                                "message",
                                0L,
                                "reason",
                                "status2",
                                "type"),
                        new Condition(
                                existingConditionLastTransitionTime,
                                "message",
                                0L,
                                "reason",
                                "status2",
                                "type"),
                        false),

                // Test Multiple Conditions with Multiple Types
                Arguments.of(
                        List.of(
                                new Condition("time1", "message", 0L, "reason", "status", "type1"),
                                new Condition("time2", "message", 0L, "reason", "status", "type1"),
                                new Condition("time3", "message", 0L, "reason", "status", "type2")),
                        new Condition("time1", "message", 0L, "reason", "status", "type1"),
                        new Condition(
                                "lastTransitionTime", "message", 0L, "reason", "status", "type1"),
                        true),

                // Test No Matching Type
                Arguments.of(
                        List.of(
                                new Condition(
                                        existingConditionLastTransitionTime,
                                        "message",
                                        0L,
                                        "reason",
                                        "status",
                                        "type1")),
                        new Condition(
                                existingConditionLastTransitionTime,
                                "message",
                                0L,
                                "reason",
                                "status",
                                "type2"),
                        new Condition(
                                "lastTransitionTime", "message", 0L, "reason", "status", "type2"),
                        false));
    }

    @ParameterizedTest
    @MethodSource("updateLastTransitionTimeParams")
    void testUpdateLastTransitionTime(
            List<Condition> conditions, Condition expected, Condition actual, boolean equals) {
        ConditionsUtils.updateLastTransitionTime(conditions, actual);

        assertEquals(expected.getMessage(), actual.getMessage());
        assertEquals(expected.getObservedGeneration(), actual.getObservedGeneration());
        assertEquals(expected.getReason(), actual.getReason());
        assertEquals(expected.getStatus(), actual.getStatus());
        assertEquals(expected.getType(), actual.getType());

        if (equals) {
            assertEquals(expected.getLastTransitionTime(), actual.getLastTransitionTime());
        } else {
            assertNotEquals(expected.getLastTransitionTime(), actual.getLastTransitionTime());
        }
    }

    @Test
    void testNullUpdateLastTransitionTime() {
        List<Condition> conditions = null;
        Condition condition = null;

        assertDoesNotThrow(() -> ConditionsUtils.updateLastTransitionTime(conditions, condition));

        assertNull(condition);
    }

    @Test
    void testCreateApplicationModeCondition() {
        Condition condition = ConditionsUtils.createApplicationModeCondition(JobStatus.RUNNING);

        assertNotNull(condition);
        assertEquals(FlinkDeploymentStatus.CONDITION_TYPE_RUNNING, condition.getType());
        assertEquals("True", condition.getStatus());
        assertEquals("Running", condition.getReason());
        assertEquals("Job status RUNNING", condition.getMessage());
    }

    @Test
    void testCreateSessionModeCondition() {
        Condition condition =
                ConditionsUtils.createSessionModeCondition(JobManagerDeploymentStatus.READY);

        assertNotNull(condition);
        assertEquals(FlinkDeploymentStatus.CONDITION_TYPE_RUNNING, condition.getType());
        assertEquals("True", condition.getStatus());
        assertEquals("JobManagerReady", condition.getReason());
        assertEquals(
                "JobManager is running and ready to receive REST API calls",
                condition.getMessage());
    }
}
