<?php

namespace Tests\Feature;

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Tests\TestCase;

class AuthorizationRoleCheckAuditPathNormalizationTest extends TestCase
{
    public function test_relative_directory_prefix_can_approve_a_temporary_role_check_bridge(): void
    {
        $directory = storage_path('framework/testing/authorization-role-prefix-audit');
        $source = $directory.'/TemporaryBridge.php';
        $reportPath = storage_path('framework/testing/authorization-role-prefix-audit.json');

        File::deleteDirectory($directory);
        File::ensureDirectoryExists($directory);
        File::put($source, "<?php\n\nreturn \$user->hasAnyRole(['branch-manager', 'cashier']);\n");
        File::delete($reportPath);

        config()->set('authorization_role_check_audit.source_scan_paths', [$directory]);
        config()->set('authorization_role_check_audit.approved_role_check_paths', [
            'storage/framework/testing/authorization-role-prefix-audit/' => 'Temporary compatibility bridge covered by Phase 9 cleanup.',
        ]);
        config()->set('authorization_role_check_audit.structural_role_reference_paths', []);

        try {
            $exitCode = Artisan::call('authorization:audit-role-checks', [
                '--strict' => true,
                '--output' => $reportPath,
            ]);

            $this->assertSame(0, $exitCode, Artisan::output());
            $report = json_decode(File::get($reportPath), true, flags: JSON_THROW_ON_ERROR);

            $this->assertSame('pass', $report['status']);
            $this->assertSame(1, $report['summary']['authorization_checks']);
            $this->assertSame(0, $report['summary']['unapproved_authorization_checks']);
            $this->assertTrue($report['call_sites'][0]['approved']);
        } finally {
            File::delete($reportPath);
            File::deleteDirectory($directory);
        }
    }
}
