// 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 jakarta.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import static jakarta.servlet.http.HttpServletResponse.SC_NO_CONTENT;
import static jakarta.servlet.http.HttpServletResponse.SC_OK;
import static jakarta.servlet.http.HttpServletResponse.SC_SERVICE_UNAVAILABLE;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.google.api.services.dataflow.Dataflow;
import com.google.api.services.dataflow.Dataflow.Projects;
import com.google.api.services.dataflow.Dataflow.Projects.Locations;
import com.google.api.services.dataflow.Dataflow.Projects.Locations.Jobs;
import com.google.api.services.dataflow.Dataflow.Projects.Locations.Jobs.Get;
import com.google.api.services.dataflow.model.Job;
import com.google.cloud.tasks.v2.HttpMethod;
import com.google.common.net.MediaType;
import google.registry.batch.CloudTasksUtils;
import google.registry.testing.CloudTasksHelper;
import google.registry.testing.CloudTasksHelper.TaskMatcher;
import google.registry.testing.FakeResponse;
import java.io.IOException;
import java.time.YearMonth;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/** Unit tests for {@link PublishInvoicesAction}. */
class PublishInvoicesActionTest {

  private final Dataflow dataflow = mock(Dataflow.class);
  private final Projects projects = mock(Projects.class);
  private final Locations locations = mock(Locations.class);
  private final Jobs jobs = mock(Jobs.class);
  private final Get get = mock(Get.class);
  private final Job expectedJob = new Job();
  private final BillingEmailUtils emailUtils = mock(BillingEmailUtils.class);
  private final FakeResponse response = new FakeResponse();
  private final CloudTasksHelper cloudTasksHelper = new CloudTasksHelper();
  private final CloudTasksUtils cloudTasksUtils = cloudTasksHelper.getTestCloudTasksUtils();
  private PublishInvoicesAction uploadAction;

  @BeforeEach
  void beforeEach() throws IOException {
    when(dataflow.projects()).thenReturn(projects);
    when(projects.locations()).thenReturn(locations);
    when(locations.jobs()).thenReturn(jobs);
    when(jobs.get("test-project", "test-region", "12345")).thenReturn(get);
    when(get.execute()).thenReturn(expectedJob);

    uploadAction =
        new PublishInvoicesAction(
            "test-project",
            "test-region",
            "12345",
            emailUtils,
            dataflow,
            response,
            YearMonth.of(2017, 10),
            cloudTasksUtils);
  }

  @Test
  void testJobDone_enqueuesCopyAction_emailsResults() {
    expectedJob.setCurrentState("JOB_STATE_DONE");
    uploadAction.run();
    assertThat(response.getStatus()).isEqualTo(SC_OK);
    verify(emailUtils).emailOverallInvoice();
    TaskMatcher matcher =
        new TaskMatcher()
            .path("/_dr/task/copyDetailReports")
            .method(HttpMethod.POST)
            .param("yearMonth", "2017-10");
    cloudTasksHelper.assertTasksEnqueued("retryable-cron-tasks", matcher);
  }

  @Test
  void testJobFailed_returnsNonRetriableResponse() {
    expectedJob.setCurrentState("JOB_STATE_FAILED");
    uploadAction.run();
    assertThat(response.getStatus()).isEqualTo(SC_NO_CONTENT);
    verify(emailUtils).sendAlertEmail("Dataflow job 12345 ended in status failure.");
  }

  @Test
  void testJobIndeterminate_returnsRetriableResponse() {
    expectedJob.setCurrentState("JOB_STATE_RUNNING");
    uploadAction.run();
    assertThat(response.getStatus()).isEqualTo(SC_SERVICE_UNAVAILABLE);
  }

  @Test
  void testIOException_returnsFailureMessage() throws IOException {
    when(get.execute()).thenThrow(new IOException("expected"));
    uploadAction.run();
    assertThat(response.getStatus()).isEqualTo(SC_INTERNAL_SERVER_ERROR);
    assertThat(response.getContentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8);
    assertThat(response.getPayload()).isEqualTo("Template launch failed: expected");
    verify(emailUtils).sendAlertEmail("Publish action failed due to expected");
  }
}
