<?php
namespace App\Application\Services\Payouts;
use DomainException;
final class PayoutStateMachine {
 private const MAP=['requested'=>['pending_2fa','cancelled','rejected'],'pending_2fa'=>['pending_screening','cancelled','rejected'],'pending_screening'=>['pending_approval','manual_review','rejected','failed'],'pending_approval'=>['approved','rejected','cancelled','manual_review'],'approved'=>['broadcasting','cancelled'],'broadcasting'=>['confirming','failed','manual_review'],'confirming'=>['completed','failed','manual_review'],'manual_review'=>['pending_approval','rejected','failed']];
 public function transition(object $p,string $to):void{if($p->status===$to)return;if(!in_array($to,self::MAP[$p->status]??[],true))throw new DomainException("Invalid payout transition {$p->status} -> {$to}");$p->status=$to;$p->save();}
 public function terminal(string $s):bool{return in_array($s,['completed','rejected','failed','cancelled'],true);}
}
