/*
 * Copyright 2025 Adobe. All rights reserved.
 * This file is licensed to you 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 REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

import { vi } from "vitest";

/**
 * Sets up a fake environment used within `aio app dev` runs.
 * The environment mimics the environment used since `aio-app-plugin-app-dev@2.1.1`
 *
 * @see https://github.com/adobe/aio-cli-plugin-app-dev/releases/tag/2.1.1
 */
export function setupDevelopmentEnv() {
  // All the "legacy" env vars
  setupLegacyDevelopmentEnv();

  // Plus some overrides that start since v2.1.1:
  vi.stubEnv("AIO_DEV", "true");
  vi.stubEnv(
    "__OW_ACTION_NAME",
    `/${process.env.__OW_NAMESPACE}/test-dev-package-name/test-dev-action-name`,
  );
}

/** Sets up a fake environment used within `aio app dev` runs. */
export function setupLegacyDevelopmentEnv() {
  // In older versions of the AIO runtime, the AIO_DEV environment variable was not set
  // This "legacy" fixture is used to test the behavior of the library in these older versions
  vi.stubEnv("__OW_NAMESPACE", "test-dev-namespace");
  vi.stubEnv("__OW_ACTIVATION_ID", "test-dev-activation-id");
  vi.stubEnv("__OW_ACTION_NAME", "test-dev-action-name");
}

/** Sets up a fake environment used within deployed runtime action runs. */
export function setupProductionEnv() {
  vi.stubEnv("__OW_NAMESPACE", "test-prod-namespace");
  vi.stubEnv("__OW_ACTIVATION_ID", "test-prod-activation-id");
  vi.stubEnv("__OW_REGION", "test-prod-region");
  vi.stubEnv("__OW_TRANSACTION_ID", "test-prod-transaction-id");
  vi.stubEnv("__OW_ACTION_VERSION", "1.0.0");
  vi.stubEnv(
    "__OW_ACTION_NAME",
    "/test-prod-namespace/test-prod-package-name/test-prod-action-name",
  );

  // The `__OW_DEADLINE` env var represents the timestamp (in ms)
  // by which the action execution must be completed.
  const oneMonthInMilliseconds = 2_592_000_000;
  vi.stubEnv("__OW_DEADLINE", `${Date.now() + oneMonthInMilliseconds}`);
}
