<?php

declare( strict_types=1 );

namespace Simplyin\Simplyin_W_Plugin\Tests\Unit;

use PHPUnit\Framework\TestCase;
use Simplyin_Env;

require_once __DIR__ . '/../../Simplyin_Env.php';

/**
 * Simplyin_Env::resolve_backend_url() is the pure, bootstrap-free env-to-URL mapping used
 * by uninstall.php (which WordPress runs without the plugin's main file, so it cannot rely
 * on `simplyin()` / $simplyin_config having been set up). Exercises every APP_ENV branch.
 */
final class Simplyin_Env_Test extends TestCase {

	public function test_resolves_prod_url_for_prod_env(): void {
		$this->assertSame(
			'https://prod.example/api',
			Simplyin_Env::resolve_backend_url(
				[
					'APP_ENV'                  => 'prod',
					'BACKEND_ENVIRONMENT_PROD' => 'https://prod.example/api',
				]
			)
		);
	}

	public function test_resolves_stage_url_for_stage_env(): void {
		$this->assertSame(
			'https://stage.example/api',
			Simplyin_Env::resolve_backend_url(
				[
					'APP_ENV'                    => 'stage',
					'BACKEND_ENVIRONMENT_STAGE'  => 'https://stage.example/api',
					'BACKEND_ENVIRONMENT_PROD'   => 'https://prod.example/api',
				]
			)
		);
	}

	public function test_resolves_dev_url_for_dev_env(): void {
		$this->assertSame(
			'https://dev.example/api',
			Simplyin_Env::resolve_backend_url(
				[
					'APP_ENV'                 => 'dev',
					'BACKEND_ENVIRONMENT_DEV' => 'https://dev.example/api',
				]
			)
		);
	}

	public function test_resolves_preprod_url_for_preprod_env(): void {
		$this->assertSame(
			'https://preprod.example/api',
			Simplyin_Env::resolve_backend_url(
				[
					'APP_ENV'                     => 'preprod',
					'BACKEND_ENVIRONMENT_PREPROD' => 'https://preprod.example/api',
				]
			)
		);
	}

	public function test_falls_back_to_prod_for_unknown_or_missing_app_env(): void {
		$this->assertSame(
			'https://prod.example/api',
			Simplyin_Env::resolve_backend_url(
				[ 'BACKEND_ENVIRONMENT_PROD' => 'https://prod.example/api' ]
			)
		);
	}

	public function test_defaults_to_read_when_no_env_given(): void {
		// get_default()'s baked APP_ENV is 'prod' with a real prod URL — this is the actual
		// value shipped to production when no build-time env substitution has run.
		$this->assertSame( 'https://prod.backend.simply.in/api', Simplyin_Env::resolve_backend_url() );
	}
}
