<?php

use App\Models\Tenant;
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 = Tenant::create([
        'tenant_id' => 'tenant-env',
        'name' => 'Preferred Tenant',
        'is_current' => false,
    ]);

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

    $resolved = Tenant::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 () => Tenant::current())->toThrow(
        \RuntimeException::class,
        'Configured INTUNE_TENANT_ID tenant 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 = Tenant::create([
        'tenant_id' => 'tenant-current',
        'name' => 'Current Tenant',
        'is_current' => true,
    ]);

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

    $resolved = Tenant::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=');

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

    expect(fn () => Tenant::current())->toThrow(\RuntimeException::class, 'No current tenant selected.');

    restoreIntuneTenantId($originalEnv);
});

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

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

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

    $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 = Tenant::create([
        'tenant_id' => 'tenant-current',
        'name' => 'Already Current',
        'is_current' => true,
    ]);

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

    $current->makeCurrent();

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

    restoreIntuneTenantId($originalEnv);
});
