<?php

use App\Models\ManagedEnvironment;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);
function restoreIntuneTenantId(string|false $original): void
{
    $original !== false
        ? putenv("INTUNE_TENANT_ID={$original}")
        : putenv('INTUNE_TENANT_ID');
}

it('returns env-configured tenant when active', function () {
    $originalEnv = getenv('INTUNE_TENANT_ID');
    putenv('INTUNE_TENANT_ID=tenant-env');

    $preferred = ManagedEnvironment::create([
        'managed_environment_id' => 'tenant-env',
        'name' => 'Preferred ManagedEnvironment',
        'is_current' => false,
    ]);

    ManagedEnvironment::create([
        'managed_environment_id' => 'other-tenant',
        'name' => 'Other ManagedEnvironment',
        'is_current' => true,
    ]);

    $resolved = ManagedEnvironment::current();

    expect($resolved->id)->toBe($preferred->id);

    restoreIntuneTenantId($originalEnv);
});

it('throws when env tenant is missing or inactive', function () {
    $originalEnv = getenv('INTUNE_TENANT_ID');
    putenv('INTUNE_TENANT_ID=missing-tenant');

    expect(fn () => ManagedEnvironment::current())->toThrow(
        \RuntimeException::class,
        'Configured INTUNE_TENANT_ID managed environment is missing or inactive.'
    );

    restoreIntuneTenantId($originalEnv);
});

it('returns tenant marked as current when no env override', function () {
    $originalEnv = getenv('INTUNE_TENANT_ID');
    putenv('INTUNE_TENANT_ID=');

    $current = ManagedEnvironment::create([
        'managed_environment_id' => 'tenant-current',
        'name' => 'Current ManagedEnvironment',
        'is_current' => true,
    ]);

    ManagedEnvironment::create([
        'managed_environment_id' => 'tenant-inactive',
        'name' => 'Inactive Current Flag',
        'status' => 'archived',
    ]);

    $resolved = ManagedEnvironment::current();

    expect($resolved->id)->toBe($current->id);

    restoreIntuneTenantId($originalEnv);
});

it('throws when no current tenant is selected', function () {
    $originalEnv = getenv('INTUNE_TENANT_ID');
    putenv('INTUNE_TENANT_ID=');

    ManagedEnvironment::create([
        'managed_environment_id' => 'tenant-one',
        'name' => 'ManagedEnvironment One',
        'is_current' => false,
    ]);

    expect(fn () => ManagedEnvironment::currentOrFail())->toThrow(\RuntimeException::class, 'No current managed environment selected.');

    restoreIntuneTenantId($originalEnv);
});

it('makeCurrent toggles flags within a transaction', function () {
    $originalEnv = getenv('INTUNE_TENANT_ID');
    putenv('INTUNE_TENANT_ID=');

    $first = ManagedEnvironment::create([
        'managed_environment_id' => 'tenant-first',
        'name' => 'First ManagedEnvironment',
        'is_current' => true,
    ]);

    $second = ManagedEnvironment::create([
        'managed_environment_id' => 'tenant-second',
        'name' => 'Second ManagedEnvironment',
    ]);

    $second->makeCurrent();

    expect($second->fresh()->is_current)->toBeTrue();
    expect($first->fresh()->is_current)->toBeFalse();

    restoreIntuneTenantId($originalEnv);
});

it('makeCurrent keeps tenant current when already current', function () {
    $originalEnv = getenv('INTUNE_TENANT_ID');
    putenv('INTUNE_TENANT_ID=');

    $current = ManagedEnvironment::create([
        'managed_environment_id' => 'tenant-current',
        'name' => 'Already Current',
        'is_current' => true,
    ]);

    $other = ManagedEnvironment::create([
        'managed_environment_id' => 'tenant-other',
        'name' => 'Other ManagedEnvironment',
        'is_current' => false,
    ]);

    $current->makeCurrent();

    expect($current->fresh()->is_current)->toBeTrue();
    expect($other->fresh()->is_current)->toBeFalse();

    restoreIntuneTenantId($originalEnv);
});
