<?php

use App\Models\Tenant;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Str;

uses(RefreshDatabase::class);

test('policies seeder assigns uuid v4 external_id to seeded tenant', function (): void {
    config()->set('tenantpilot.supported_policy_types', []);

    $_ENV['INTUNE_TENANT_ID'] = 'seed-tenant-id';
    $_SERVER['INTUNE_TENANT_ID'] = 'seed-tenant-id';
    putenv('INTUNE_TENANT_ID=seed-tenant-id');

    $this->artisan('db:seed', ['--class' => Database\Seeders\PoliciesSeeder::class])
        ->assertExitCode(0);

    $tenant = Tenant::query()->first();

    expect($tenant)->not->toBeNull();
    expect($tenant?->tenant_id)->toBe('seed-tenant-id');

    $externalId = (string) ($tenant?->external_id ?? '');

    expect(Str::isUuid($externalId))->toBeTrue();
    expect(substr($externalId, 14, 1))->toBe('4');
    expect(in_array(strtolower(substr($externalId, 19, 1)), ['8', '9', 'a', 'b'], true))->toBeTrue();
});
