// Copyright 2017 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.model.transfer;

import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.ImmutableSet;
import google.registry.model.billing.BillingCancellation;
import google.registry.model.billing.BillingEvent;
import google.registry.model.billing.BillingRecurrence;
import google.registry.model.domain.Period;
import google.registry.model.eppcommon.Trid;
import google.registry.model.poll.PollMessage;
import google.registry.persistence.VKey;
import java.time.Instant;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/** Unit tests for {@link DomainTransferData}. */
public class DomainTransferDataTest {

  private final Instant now = Instant.parse("2024-03-27T10:15:30.105Z");

  private VKey<BillingEvent> transferBillingEventKey;
  private VKey<BillingCancellation> otherServerApproveBillingEventKey;
  private VKey<BillingRecurrence> recurrenceKey;
  private VKey<PollMessage.Autorenew> autorenewPollMessageKey;
  private VKey<PollMessage.OneTime> otherServerApprovePollMessageKey;

  @BeforeEach
  void beforeEach() {
    transferBillingEventKey = BillingEvent.createVKey(12345L);
    otherServerApproveBillingEventKey = BillingCancellation.createVKey(2468L);
    recurrenceKey = BillingRecurrence.createVKey(13579L);
    autorenewPollMessageKey = VKey.create(PollMessage.Autorenew.class, 67890L);
    otherServerApprovePollMessageKey = VKey.create(PollMessage.OneTime.class, 314159L);
  }

  @Test
  void test_copyConstantFieldsToBuilder() {
    DomainTransferData constantTransferData =
        new DomainTransferData.Builder()
            .setTransferRequestTrid(Trid.create("server-trid", "client-trid"))
            .setTransferRequestTime(now)
            .setGainingRegistrarId("NewRegistrar")
            .setLosingRegistrarId("TheRegistrar")
            // Test must use a non-1-year period, since that's the default value.
            .setTransferPeriod(Period.create(5, Period.Unit.YEARS))
            .build();
    DomainTransferData fullTransferData =
        constantTransferData
            .asBuilder()
            .setPendingTransferExpirationTime(now)
            .setTransferStatus(TransferStatus.PENDING)
            .setServerApproveEntities(
                "4-TLD",
                1356L,
                ImmutableSet.of(
                    transferBillingEventKey,
                    otherServerApproveBillingEventKey,
                    recurrenceKey,
                    autorenewPollMessageKey,
                    otherServerApprovePollMessageKey))
            .setServerApproveBillingEvent(transferBillingEventKey)
            .setServerApproveAutorenewEvent(recurrenceKey)
            .setServerApproveAutorenewPollMessage(autorenewPollMessageKey)
            .build();
    // asBuilder() copies over all fields
    assertThat(fullTransferData.asBuilder().build()).isEqualTo(fullTransferData);
    // copyConstantFieldsToBuilder() copies only constant fields
    assertThat(fullTransferData.copyConstantFieldsToBuilder().build())
        .isEqualTo(constantTransferData);
  }
}
