// 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.reporting.billing;

import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.common.Cursor.CursorType.RECURRING_BILLING;
import static google.registry.testing.DatabaseHelper.persistResource;
import static jakarta.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import static jakarta.servlet.http.HttpServletResponse.SC_OK;
import static org.mockito.ArgumentMatchers.startsWith;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.google.cloud.tasks.v2.HttpMethod;
import com.google.common.net.MediaType;
import google.registry.batch.CloudTasksUtils;
import google.registry.beam.BeamActionTestBase;
import google.registry.model.common.Cursor;
import google.registry.persistence.transaction.JpaTestExtensions;
import google.registry.persistence.transaction.JpaTestExtensions.JpaIntegrationTestExtension;
import google.registry.reporting.ReportingModule;
import google.registry.testing.CloudTasksHelper;
import google.registry.testing.CloudTasksHelper.TaskMatcher;
import google.registry.testing.FakeClock;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.time.YearMonth;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

/** Unit tests for {@link google.registry.reporting.billing.GenerateInvoicesAction}. */
class GenerateInvoicesActionTest extends BeamActionTestBase {

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

  private final BillingEmailUtils emailUtils = mock(BillingEmailUtils.class);
  private FakeClock clock = new FakeClock();
  private CloudTasksHelper cloudTasksHelper = new CloudTasksHelper();
  private CloudTasksUtils cloudTasksUtils = cloudTasksHelper.getTestCloudTasksUtils();
  private GenerateInvoicesAction action;

  private void setCursor(Instant cursorTime) {
    persistResource(Cursor.createGlobal(RECURRING_BILLING, cursorTime));
  }

  @Test
  void testLaunchTemplateJob_withPublish() throws Exception {
    setCursor(Instant.parse("2017-11-01T00:00:00Z"));
    action =
        new GenerateInvoicesAction(
            "test-project",
            "test-region",
            "staging_bucket",
            "billing_bucket",
            "REG-INV",
            true,
            YearMonth.of(2017, 10),
            emailUtils,
            cloudTasksUtils,
            clock,
            response,
            dataflow);
    action.run();
    assertThat(response.getContentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8);
    assertThat(response.getStatus()).isEqualTo(SC_OK);
    assertThat(response.getPayload()).isEqualTo("Launched invoicing pipeline: jobid");

    cloudTasksHelper.assertTasksEnqueued(
        "beam-reporting",
        new TaskMatcher()
            .path("/_dr/task/publishInvoices")
            .method(HttpMethod.POST)
            .param("jobId", "jobid")
            .param("yearMonth", "2017-10")
            .scheduleTime(
                clock.now().plus(Duration.ofMinutes(ReportingModule.ENQUEUE_DELAY_MINUTES))));
  }

  @Test
  void testLaunchTemplateJob_withoutPublish() throws Exception {
    setCursor(Instant.parse("2017-11-01T00:00:00Z"));
    action =
        new GenerateInvoicesAction(
            "test-project",
            "test-region",
            "staging_bucket",
            "billing_bucket",
            "REG-INV",
            false,
            YearMonth.of(2017, 10),
            emailUtils,
            cloudTasksUtils,
            clock,
            response,
            dataflow);
    action.run();
    assertThat(response.getContentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8);
    assertThat(response.getStatus()).isEqualTo(SC_OK);
    assertThat(response.getPayload()).isEqualTo("Launched invoicing pipeline: jobid");
    cloudTasksHelper.assertNoTasksEnqueued("beam-reporting");
  }

  @Test
  void testCaughtIOException() throws IOException {
    setCursor(Instant.parse("2017-11-01T00:00:00Z"));
    when(launch.execute()).thenThrow(new IOException("Pipeline error"));
    action =
        new GenerateInvoicesAction(
            "test-project",
            "test-region",
            "staging_bucket",
            "billing_bucket",
            "REG-INV",
            false,
            YearMonth.of(2017, 10),
            emailUtils,
            cloudTasksUtils,
            clock,
            response,
            dataflow);
    action.run();
    assertThat(response.getStatus()).isEqualTo(SC_INTERNAL_SERVER_ERROR);
    assertThat(response.getPayload()).isEqualTo("Pipeline launch failed: Pipeline error");
    verify(emailUtils).sendAlertEmail("Pipeline Launch failed due to Pipeline error");
    cloudTasksHelper.assertNoTasksEnqueued("beam-reporting");
  }

  @Test
  void testFailure_cursorLagging() {
    setCursor(Instant.parse("2017-10-31T23:59:59.999Z"));
    action =
        new GenerateInvoicesAction(
            "test-project",
            "test-region",
            "staging_bucket",
            "billing_bucket",
            "REG-INV",
            false,
            YearMonth.of(2017, 10),
            emailUtils,
            cloudTasksUtils,
            clock,
            response,
            dataflow);
    action.run();
    assertThat(response.getStatus()).isEqualTo(SC_INTERNAL_SERVER_ERROR);
    assertThat(response.getPayload()).contains("Pipeline launch failed");
    assertThat(response.getPayload()).contains("BillingRecurrence expansion cursor");
    verify(emailUtils)
        .sendAlertEmail(
            startsWith("Pipeline Launch failed due to BillingRecurrence expansion cursor"));
    cloudTasksHelper.assertNoTasksEnqueued("beam-reporting");
  }

  @Test
  void testFailure_cursorMissing() {
    // Do not set cursor, should default to START_INSTANT (1970)
    action =
        new GenerateInvoicesAction(
            "test-project",
            "test-region",
            "staging_bucket",
            "billing_bucket",
            "REG-INV",
            false,
            YearMonth.of(2017, 10),
            emailUtils,
            cloudTasksUtils,
            clock,
            response,
            dataflow);
    action.run();
    assertThat(response.getStatus()).isEqualTo(SC_INTERNAL_SERVER_ERROR);
    assertThat(response.getPayload()).contains("Pipeline launch failed");
    assertThat(response.getPayload()).contains("BillingRecurrence expansion cursor");
    verify(emailUtils)
        .sendAlertEmail(
            startsWith("Pipeline Launch failed due to BillingRecurrence expansion cursor"));
    cloudTasksHelper.assertNoTasksEnqueued("beam-reporting");
  }
}
