<?php
declare(strict_types=1);
namespace App\Services\Trust;

final class TrustScoreCalculator
{
    /** @param array<string,array{ok:bool,weight:int}> $criteria @return array{score:int,level:string,decision:string,breakdown:array<string,mixed>} */
    public function calculate(array $criteria): array
    {
        $score=0;$max=0;$breakdown=[];
        foreach($criteria as $name=>$criterion){$weight=max(0,(int)$criterion['weight']);$max+=$weight;$earned=$criterion['ok']?$weight:0;$score+=$earned;$breakdown[$name]=['ok'=>$criterion['ok'],'weight'=>$weight,'earned'=>$earned];}
        $normalized=$max>0?(int)round(($score/$max)*100):0;
        $level=$normalized>=95?'Enterprise Trusted':($normalized>=80?'Trusted':($normalized>=65?'Verified':($normalized>=40?'Warning':'Rejected')));
        $decision=$normalized>=65?'TRUSTED':($normalized>=40?'WARNING':'REJECTED');
        return ['score'=>$normalized,'level'=>$level,'decision'=>$decision,'breakdown'=>$breakdown];
    }
}
