<?php

declare(strict_types=1);

namespace App\Services\PermissionPosture;

/**
 * Calculates a normalized posture score (0–100) from a permission comparison result.
 *
 * Pure function: no side effects, no DB access.
 */
final class PostureScoreCalculator
{
    /**
     * @param  array{permissions: array<int, array{status: string, ...}>, ...}  $permissionComparison
     */
    public function calculate(array $permissionComparison): int
    {
        $permissions = $permissionComparison['permissions'] ?? [];

        if (! is_array($permissions)) {
            return 100;
        }

        $requiredCount = count($permissions);

        if ($requiredCount === 0) {
            return 100;
        }

        $grantedCount = 0;

        foreach ($permissions as $permission) {
            if (is_array($permission) && ($permission['status'] ?? null) === 'granted') {
                $grantedCount++;
            }
        }

        return (int) round($grantedCount / $requiredCount * 100);
    }
}
