<?php
namespace App\Services;
use App\Models\{PointEntry,PointRule,User};
class Points {
 public function award(User $u,string $rule,string $reference):void {$r=PointRule::where('code',$rule)->where('active',true)->first();if($r){$entry=PointEntry::firstOrCreate(['event_key'=>hash('sha256',"$rule:$u->id:$reference")],['user_id'=>$u->id,'reference'=>$reference,'points'=>$r->points,'title'=>$r->title,'rule_version'=>$r->version]);if($entry->wasRecentlyCreated)app(Notify::class)->event('points',null,['customer_name'=>$u->name,'points'=>$r->points,'action_title'=>$r->title,'notification_url'=>url('/club')],"points:$entry->id",$u);}}
 public function adjust(User $u,int $amount,string $reason,User $actor,string $key):void {abort_unless($actor->canAdmin('points'),403);PointEntry::firstOrCreate(['event_key'=>hash('sha256',$key)],['user_id'=>$u->id,'actor_id'=>$actor->id,'reference'=>'manual','points'=>$amount,'title'=>$reason]);}
}
