// Copyright 2024 The Nomulus Authors. All Rights Reserved.
//
// 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 google.registry.tools;

import static google.registry.model.common.FeatureFlag.FeatureName.MINIMUM_DATASET_CONTACTS_OPTIONAL;
import static google.registry.model.common.FeatureFlag.FeatureName.MINIMUM_DATASET_CONTACTS_PROHIBITED;
import static google.registry.model.common.FeatureFlag.FeatureName.TEST_FEATURE;
import static google.registry.model.common.FeatureFlag.FeatureStatus.ACTIVE;
import static google.registry.model.common.FeatureFlag.FeatureStatus.INACTIVE;
import static google.registry.testing.DatabaseHelper.persistFeatureFlag;
import static google.registry.testing.TestDataHelper.loadFile;
import static google.registry.util.DateTimeUtils.START_INSTANT;
import static google.registry.util.DateTimeUtils.plusWeeks;

import com.google.common.collect.ImmutableSortedMap;
import google.registry.model.EntityYamlUtils;
import google.registry.model.common.FeatureFlag.FeatureStatus;
import java.time.Instant;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class ListFeatureFlagsCommandTest extends CommandTestCase<ListFeatureFlagsCommand> {

  @BeforeEach
  void beforeEach() {
    command.mapper = EntityYamlUtils.createObjectMapper();
    fakeClock.setTo(Instant.parse("1984-12-21T06:07:08.789Z"));
  }

  @Test
  void testSuccess_oneFlag() throws Exception {
    persistFeatureFlag(TEST_FEATURE, INACTIVE, plusWeeks(fakeClock.now(), 8), ACTIVE);
    runCommand();
    assertInStdout(loadFile(getClass(), "oneFlag.yaml"));
  }

  @Test
  void test_success_manyFlags() throws Exception {
    persistFeatureFlag(TEST_FEATURE, INACTIVE, plusWeeks(fakeClock.now(), 8), ACTIVE);
    persistFeatureFlag(
        MINIMUM_DATASET_CONTACTS_OPTIONAL,
        ImmutableSortedMap.<Instant, FeatureStatus>naturalOrder()
            .put(START_INSTANT, INACTIVE)
            .put(plusWeeks(fakeClock.now(), 1), ACTIVE)
            .put(plusWeeks(fakeClock.now(), 8), INACTIVE)
            .put(plusWeeks(fakeClock.now(), 10), ACTIVE)
            .build());
    persistFeatureFlag(MINIMUM_DATASET_CONTACTS_PROHIBITED, ACTIVE);
    runCommand();
    assertInStdout(loadFile(getClass(), "threeFlags.yaml"));
  }
}
