// 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.rde;

import static com.google.common.io.BaseEncoding.base16;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static google.registry.testing.DatabaseHelper.createTld;
import static google.registry.testing.DatabaseHelper.persistEppResource;
import static google.registry.testing.DatabaseHelper.persistResource;
import static google.registry.util.DateTimeUtils.END_INSTANT;
import static google.registry.xjc.rgp.XjcRgpStatusValueType.RENEW_PERIOD;
import static google.registry.xjc.rgp.XjcRgpStatusValueType.TRANSFER_PERIOD;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.joda.money.CurrencyUnit.USD;

import com.google.common.collect.ImmutableSet;
import com.google.common.net.InetAddresses;
import google.registry.model.billing.BillingBase.Flag;
import google.registry.model.billing.BillingBase.Reason;
import google.registry.model.billing.BillingEvent;
import google.registry.model.billing.BillingRecurrence;
import google.registry.model.domain.Domain;
import google.registry.model.domain.DomainAuthInfo;
import google.registry.model.domain.DomainHistory;
import google.registry.model.domain.GracePeriod;
import google.registry.model.domain.rgp.GracePeriodStatus;
import google.registry.model.domain.secdns.DomainDsData;
import google.registry.model.eppcommon.AuthInfo.PasswordAuth;
import google.registry.model.eppcommon.StatusValue;
import google.registry.model.eppcommon.Trid;
import google.registry.model.host.Host;
import google.registry.model.poll.PollMessage;
import google.registry.model.poll.PollMessage.Autorenew;
import google.registry.model.rde.RdeMode;
import google.registry.model.reporting.HistoryEntry;
import google.registry.model.transfer.DomainTransferData;
import google.registry.model.transfer.TransferStatus;
import google.registry.persistence.transaction.JpaTestExtensions;
import google.registry.persistence.transaction.JpaTestExtensions.JpaIntegrationTestExtension;
import google.registry.testing.DatabaseHelper;
import google.registry.testing.FakeClock;
import google.registry.util.Idn;
import google.registry.xjc.domain.XjcDomainStatusType;
import google.registry.xjc.domain.XjcDomainStatusValueType;
import google.registry.xjc.rde.XjcRdeContentsType;
import google.registry.xjc.rde.XjcRdeDeposit;
import google.registry.xjc.rde.XjcRdeDepositTypeType;
import google.registry.xjc.rde.XjcRdeMenuType;
import google.registry.xjc.rdedomain.XjcRdeDomain;
import google.registry.xjc.rdedomain.XjcRdeDomainElement;
import google.registry.xjc.rgp.XjcRgpStatusType;
import google.registry.xjc.secdns.XjcSecdnsDsDataType;
import google.registry.xml.XmlException;
import java.io.ByteArrayOutputStream;
import java.time.Instant;
import org.joda.money.Money;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

/**
 * Unit tests for {@link DomainToXjcConverter}.
 *
 * <p>This tests the mapping between {@link Domain} and {@link XjcRdeDomain} as well as some
 * exceptional conditions.
 */
public class DomainToXjcConverterTest {

  @RegisterExtension
  final JpaIntegrationTestExtension jpa =
      new JpaTestExtensions.Builder().buildIntegrationTestExtension();

  private final Instant now = Instant.parse("2014-01-01T00:00:00Z");
  private final FakeClock clock = new FakeClock(now);

  @BeforeEach
  void beforeEach() {
    createTld("xn--q9jyb4c");
  }

  @Test
  void testConvertThick() {
    XjcRdeDomain bean = DomainToXjcConverter.convertDomain(makeDomain(clock), RdeMode.FULL);

    assertThat(bean.getClID()).isEqualTo("TheRegistrar");

    assertThat(bean.getCrDate()).isEqualTo(Instant.parse("1900-01-01T00:00:00Z"));

    // o  A <crRr> element that contains the identifier of the registrar
    //    that created the domain name object.  An OPTIONAL client attribute
    //    is used to specify the client that performed the operation.
    //    This will always be null for us since we track each registrar as a separate client.
    assertThat(bean.getCrRr().getValue()).isEqualTo("TheRegistrar");
    assertThat(bean.getCrRr().getClient()).isNull();
    assertThat(bean.getExDate()).isEqualTo(Instant.parse("1930-01-01T00:00:00Z"));

    // o  An OPTIONAL <idnTableId> element that references the IDN Table
    //    used for the IDN.  This corresponds to the "id" attribute of the
    //    <idnTableRef> element.  This element MUST be present if the domain
    //    name is an IDN.
    // TODO(b/26125498): bean.getIdnTableId()

    assertThat(bean.getName()).isEqualTo("love.xn--q9jyb4c");
    assertThat(bean.getUName()).isEqualTo("love.みんな");

    assertThat(bean.getNs().getHostObjs())
        .containsExactly("bird.or.devil.xn--q9jyb4c", "ns2.cat.xn--q9jyb4c");

    // o  An OPTIONAL <originalName> element is used to indicate that the
    //    domain name is an IDN variant.  This element contains the domain
    //    name used to generate the IDN variant.
    // TODO(b/26125498): bean.getOriginalName()

    assertThat(bean.getRegistrant()).isNull();

    // o  Zero or more OPTIONAL <rgpStatus> element to represent
    //    "pendingDelete" sub-statuses, including "redemptionPeriod",
    //    "pendingRestore", and "pendingDelete", that a domain name can be
    //    in as a result of grace period processing as specified in [RFC3915].
    assertThat(bean.getRgpStatuses().stream().map(XjcRgpStatusType::getS))
        .containsExactly(TRANSFER_PERIOD, RENEW_PERIOD);

    assertWithMessage("secdns").that(bean.getSecDNS()).isNotNull();
    assertWithMessage("secdns dsdata").that(bean.getSecDNS().getDsDatas()).isNotNull();
    assertWithMessage("secdns dsdata").that(bean.getSecDNS().getDsDatas()).hasSize(1);
    XjcSecdnsDsDataType dsData = bean.getSecDNS().getDsDatas().get(0);
    assertThat(dsData.getAlg()).isEqualTo((short) 200);
    assertThat(dsData.getDigest()).isEqualTo(base16().decode("1234567890"));
    assertThat(dsData.getDigestType()).isEqualTo((short) 230);
    assertThat(dsData.getKeyTag()).isEqualTo(123);
    // TODO(b/26125499): Test dsData.getKeyData()

    assertThat(bean.getRoid()).isEqualTo("2-Q9JYB4C");

    assertThat(bean.getStatuses().stream().map(XjcDomainStatusType::getS))
        .containsExactly(
            XjcDomainStatusValueType.CLIENT_DELETE_PROHIBITED,
            XjcDomainStatusValueType.CLIENT_RENEW_PROHIBITED,
            XjcDomainStatusValueType.CLIENT_TRANSFER_PROHIBITED,
            XjcDomainStatusValueType.SERVER_UPDATE_PROHIBITED);

    assertThat(bean.getTrDate()).isEqualTo(Instant.parse("1910-01-01T00:00:00Z"));

    assertThat(bean.getTrnData().getTrStatus().toString()).isEqualTo("PENDING");
    assertThat(bean.getTrnData().getReRr().getValue()).isEqualTo("NewRegistrar");
    assertThat(bean.getTrnData().getAcRr().getValue()).isEqualTo("TheRegistrar");
    assertThat(bean.getTrnData().getAcDate()).isEqualTo(Instant.parse("1925-04-20T00:00:00Z"));
    assertThat(bean.getTrnData().getReDate()).isEqualTo(Instant.parse("1919-01-01T00:00:00Z"));
    assertThat(bean.getTrnData().getExDate()).isEqualTo(Instant.parse("1931-01-01T00:00:00Z"));

    assertThat(bean.getUpDate()).isEqualTo(Instant.parse("1920-01-01T00:00:00Z"));

    assertThat(bean.getUpRr().getValue()).isEqualTo("TheRegistrar");
    assertThat(bean.getUpRr().getClient()).isNull();
  }

  @Test
  void testConvertThin() {
    XjcRdeDomain bean = DomainToXjcConverter.convertDomain(makeDomain(clock), RdeMode.THIN);
    assertThat(bean.getRegistrant()).isNull();
    assertThat(bean.getSecDNS()).isNull();
  }

  @Test
  void testMarshalThick() throws Exception {
    XjcRdeDomain bean = DomainToXjcConverter.convertDomain(makeDomain(clock), RdeMode.FULL);
    wrapDeposit(bean).marshal(new ByteArrayOutputStream(), UTF_8);
  }

  @Test
  void testMarshalThin() throws Exception {
    XjcRdeDomain bean = DomainToXjcConverter.convertDomain(makeDomain(clock), RdeMode.THIN);
    wrapDeposit(bean).marshal(new ByteArrayOutputStream(), UTF_8);
  }

  @Test
  void testConvertAbsentContacts() throws XmlException {
    Domain domain = makeDomain(clock);
    XjcRdeDomain bean = DomainToXjcConverter.convertDomain(domain, RdeMode.FULL);
    assertThat(bean.getRegistrant()).isNull();
    wrapDeposit(bean).marshal(new ByteArrayOutputStream(), UTF_8);
  }

  XjcRdeDeposit wrapDeposit(XjcRdeDomain domain) {
    XjcRdeDeposit deposit = new XjcRdeDeposit();
    deposit.setId("984302");
    deposit.setType(XjcRdeDepositTypeType.FULL);
    deposit.setWatermark(Instant.parse("2012-01-01T04:20:00Z"));
    XjcRdeMenuType menu = new XjcRdeMenuType();
    menu.setVersion("1.0");
    menu.getObjURIs().add("lol");
    deposit.setRdeMenu(menu);
    XjcRdeDomainElement element = new XjcRdeDomainElement();
    element.setValue(domain);
    XjcRdeContentsType contents = new XjcRdeContentsType();
    contents.getContents().add(element);
    deposit.setContents(contents);
    return deposit;
  }

  static Domain makeDomain(FakeClock clock) {
    Domain domain =
        persistResource(
            DatabaseHelper.newDomain("example.xn--q9jyb4c")
                .asBuilder()
                .setRepoId("2-Q9JYB4C")
                .build());
    DomainHistory domainHistory =
        persistResource(
            new DomainHistory.Builder()
                .setModificationTime(clock.now())
                .setType(HistoryEntry.Type.DOMAIN_CREATE)
                .setDomain(domain)
                .setRegistrarId(domain.getCreationRegistrarId())
                .build());
    BillingEvent billingEvent =
        persistResource(
            new BillingEvent.Builder()
                .setReason(Reason.CREATE)
                .setTargetId("example.xn--q9jyb4c")
                .setRegistrarId("TheRegistrar")
                .setCost(Money.of(USD, 26))
                .setPeriodYears(2)
                .setEventTime(Instant.parse("1910-01-01T00:00:00Z"))
                .setBillingTime(Instant.parse("1910-01-01T00:00:00Z"))
                .setDomainHistory(domainHistory)
                .build());
    domain =
        domain
            .asBuilder()
            .setAuthInfo(DomainAuthInfo.create(PasswordAuth.create("secret")))
            .setCreationRegistrarId("TheRegistrar")
            .setCreationTimeForTest(Instant.parse("1900-01-01T00:00:00Z"))
            .setPersistedCurrentSponsorRegistrarId("TheRegistrar")
            .setDsData(
                ImmutableSet.of(DomainDsData.create(123, 200, 230, base16().decode("1234567890"))))
            .setDomainName(Idn.toASCII("love.みんな"))
            .setLastTransferTime(Instant.parse("1910-01-01T00:00:00Z"))
            .setLastEppUpdateRegistrarId("TheRegistrar")
            .setLastEppUpdateTime(Instant.parse("1920-01-01T00:00:00Z"))
            .setNameservers(
                ImmutableSet.of(
                    makeHost(clock, "3-Q9JYB4C", "bird.or.devil.みんな", "1.2.3.4").createVKey(),
                    makeHost(clock, "4-Q9JYB4C", "ns2.cat.みんな", "bad:f00d:cafe::15:beef")
                        .createVKey()))
            .setRegistrationExpirationTime(Instant.parse("1930-01-01T00:00:00Z"))
            .setGracePeriods(
                ImmutableSet.of(
                    GracePeriod.forBillingEvent(
                        GracePeriodStatus.RENEW,
                        domain.getRepoId(),
                        persistResource(
                            new BillingEvent.Builder()
                                .setReason(Reason.RENEW)
                                .setTargetId("love.xn--q9jyb4c")
                                .setRegistrarId("TheRegistrar")
                                .setCost(Money.of(USD, 456))
                                .setPeriodYears(2)
                                .setEventTime(Instant.parse("1920-01-01T00:00:00Z"))
                                .setBillingTime(Instant.parse("1920-01-01T00:00:00Z"))
                                .setDomainHistory(domainHistory)
                                .build())),
                    GracePeriod.create(
                        GracePeriodStatus.TRANSFER,
                        domain.getRepoId(),
                        Instant.parse("1920-01-01T00:00:00Z"),
                        "TheRegistrar",
                        null)))
            .setSubordinateHosts(ImmutableSet.of("home.by.horror.haunted"))
            .setStatusValues(
                ImmutableSet.of(
                    StatusValue.CLIENT_DELETE_PROHIBITED,
                    StatusValue.CLIENT_RENEW_PROHIBITED,
                    StatusValue.CLIENT_TRANSFER_PROHIBITED,
                    StatusValue.SERVER_UPDATE_PROHIBITED))
            .setAutorenewBillingEvent(
                persistResource(
                        new BillingRecurrence.Builder()
                            .setReason(Reason.RENEW)
                            .setFlags(ImmutableSet.of(Flag.AUTO_RENEW))
                            .setTargetId("lol")
                            .setRegistrarId("TheRegistrar")
                            .setEventTime(END_INSTANT)
                            .setRecurrenceEndTime(END_INSTANT)
                            .setDomainHistory(domainHistory)
                            .build())
                    .createVKey())
            .setAutorenewPollMessage(
                persistResource(
                        new PollMessage.Autorenew.Builder()
                            .setTargetId("lol")
                            .setRegistrarId("TheRegistrar")
                            .setEventTime(END_INSTANT)
                            .setAutorenewEndTime(END_INSTANT)
                            .setMsg("Domain was auto-renewed.")
                            .setHistoryEntry(domainHistory)
                            .build())
                    .createVKey())
            .setTransferData(
                new DomainTransferData.Builder()
                    .setGainingRegistrarId("NewRegistrar")
                    .setLosingRegistrarId("TheRegistrar")
                    .setPendingTransferExpirationTime(Instant.parse("1925-04-20T00:00:00Z"))
                    .setServerApproveBillingEvent(billingEvent.createVKey())
                    .setServerApproveAutorenewEvent(
                        persistResource(
                                new BillingRecurrence.Builder()
                                    .setReason(Reason.RENEW)
                                    .setFlags(ImmutableSet.of(Flag.AUTO_RENEW))
                                    .setTargetId("example.xn--q9jyb4c")
                                    .setRegistrarId("TheRegistrar")
                                    .setEventTime(END_INSTANT)
                                    .setRecurrenceEndTime(END_INSTANT)
                                    .setDomainHistory(domainHistory)
                                    .build())
                            .createVKey())
                    .setServerApproveAutorenewPollMessage(
                        persistResource(
                                new Autorenew.Builder()
                                    .setTargetId("example.xn--q9jyb4c")
                                    .setRegistrarId("TheRegistrar")
                                    .setEventTime(END_INSTANT)
                                    .setAutorenewEndTime(END_INSTANT)
                                    .setMsg("Domain was auto-renewed.")
                                    .setHistoryEntry(domainHistory)
                                    .build())
                            .createVKey())
                    .setServerApproveEntities(
                        domain.getRepoId(),
                        domainHistory.getRevisionId(),
                        ImmutableSet.of(billingEvent.createVKey()))
                    .setTransferRequestTime(Instant.parse("1919-01-01T00:00:00Z"))
                    .setTransferStatus(TransferStatus.PENDING)
                    .setTransferredRegistrationExpirationTime(Instant.parse("1931-01-01T00:00:00Z"))
                    .setTransferRequestTrid(Trid.create("client-trid", "server-trid"))
                    .build())
            .build();
    clock.advanceOneMilli();
    return persistResource(domain);
  }

  private static Host makeHost(FakeClock clock, String repoId, String fqhn, String ip) {
    clock.advanceOneMilli();
    return persistEppResource(
        new Host.Builder()
            .setCreationRegistrarId("TheRegistrar")
            .setCreationTimeForTest(Instant.parse("1900-01-01T00:00:00Z"))
            .setPersistedCurrentSponsorRegistrarId("TheRegistrar")
            .setHostName(Idn.toASCII(fqhn))
            .setInetAddresses(ImmutableSet.of(InetAddresses.forString(ip)))
            .setLastTransferTime(Instant.parse("1910-01-01T00:00:00Z"))
            .setLastEppUpdateRegistrarId("TheRegistrar")
            .setLastEppUpdateTime(Instant.parse("1920-01-01T00:00:00Z"))
            .setRepoId(repoId)
            .setStatusValues(ImmutableSet.of(StatusValue.OK))
            .build());
  }
}
