<?php
namespace App\Application\Services\Payouts;
use App\Models\{Payout,PayoutAddressAllowlist,PayoutKillSwitch};
use DomainException;
final class PayoutPolicyService {
 public function assertNoKillSwitch(Payout $p):void{$scopes=[['global',null],['network',$p->asset->network],['asset',(string)$p->asset_id],['merchant',(string)$p->merchant_id],['wallet',(string)($p->wallet_id??'')]];foreach($scopes as [$type,$id]){if(PayoutKillSwitch::where('enabled',true)->where('scope_type',$type)->when($id!==null,fn($q)=>$q->where('scope_id',$id))->where(fn($q)=>$q->whereNull('environment')->orWhere('environment',$p->environment))->exists())throw new DomainException("Payouts disabled by {$type} kill switch.");}}
 public function assertAllowlisted(Payout $p):void{$ok=PayoutAddressAllowlist::where(['merchant_id'=>$p->merchant_id,'asset_id'=>$p->asset_id,'environment'=>$p->environment,'address'=>$p->to_address,'status'=>'active'])->where('available_at','<=',now())->exists();if(!$ok)throw new DomainException('Destination address is not active on the merchant allowlist or is still cooling.');}
}
