<?php
require __DIR__.'/../../vendor/autoload.php';
$app = require __DIR__.'/../../bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
function req($method,$url,$headers=[],$body=null){$raw=$body===null?'':json_encode($body,JSON_UNESCAPED_SLASHES);$headers[]='Accept: application/json';if($raw!=='')$headers[]='Content-Type: application/json';$ch=curl_init($url);curl_setopt_array($ch,[CURLOPT_RETURNTRANSFER=>true,CURLOPT_HEADER=>true,CURLOPT_CUSTOMREQUEST=>$method,CURLOPT_TIMEOUT=>45,CURLOPT_HTTPHEADER=>$headers]);if($raw!=='')curl_setopt($ch,CURLOPT_POSTFIELDS,$raw);$res=curl_exec($ch);$err=curl_error($ch);$status=curl_getinfo($ch,CURLINFO_RESPONSE_CODE);$hs=curl_getinfo($ch,CURLINFO_HEADER_SIZE);curl_close($ch);$text=$res===false?'':substr($res,$hs);$json=json_decode($text,true);return ['status'=>$status,'body'=>$text,'json'=>json_last_error()===JSON_ERROR_NONE?$json:null,'error'=>$err];}
function hmacReq($method,$base,$path,$key,$secret,$body=null){$raw=$body===null?'':json_encode($body,JSON_UNESCAPED_SLASHES);$ts=(string)time();$nonce=bin2hex(random_bytes(12));$canonical=implode("\n",[$method,$path,$ts,$nonce,hash('sha256',$raw)]);$sig=hash_hmac('sha256',$canonical,$secret);return req($method,$base.$path,['x-api-key: '.$key,'x-api-timestamp: '.$ts,'x-api-nonce: '.$nonce,'x-api-signature: v1='.$sig],$body);}
function add(&$rows,$name,$r,$expect=[200]){$ok=in_array($r['status'],$expect,true);$rows[]=[$name,$ok,$r['status'],$ok?'':(($r['json']['error']['code']??'').': '.($r['json']['error']['message']??$r['error']??substr($r['body'],0,160)))];}
$base='http://127.0.0.1:8001';$rows=[];
$r=req('POST',$base.'/api/v1/auth/login',[],['email'=>'ahmadshahzaib2490+3@gmail.com','password'=>'waqas123waqas']);add($rows,'Merchant login',$r);$token=$r['json']['data']['token']??'';
$r=req('POST',$base.'/api/v1/auth/login',[],['email'=>'admin@prolificex.local','password'=>'Admin12345678']);add($rows,'Admin login',$r);$admin=$r['json']['data']['token']??'';
$r=req('POST',$base.'/api/v1/api-keys',['Authorization: Bearer '.$token],['environment'=>'sandbox','scopes'=>['payments:read','payments:write','balances:read','payouts:read','payouts:write','webhooks:write','wallets:read','merchants:read','merchants:write']]);add($rows,'Create API key',$r,[201]);$key=$r['json']['data']['public_key']??'';$secret=$r['json']['data']['secret']??'';
add($rows,'Dashboard summary',hmacReq('GET',$base,'/api/v1/dashboard/summary',$key,$secret));
add($rows,'Webhook probe httpbin',req('POST',$base.'/api/v1/webhooks/probe',['Authorization: Bearer '.$token],['url'=>'https://httpbin.org/post','environment'=>'sandbox']));
app(App\Jobs\OperationsMonitorJob::class)->handle();
add($rows,'Operations alerts list',req('GET',$base.'/api/v1/operator/operations/alerts',['Authorization: Bearer '.$admin]));
$passed=count(array_filter($rows,fn($r)=>$r[1]));$failed=count($rows)-$passed;$stamp=date('Ymd_His');$md="# Sandbox Extra Scenario Report\n\nPassed: {$passed}\nFailed: {$failed}\n\n| # | Scenario | Result | Status | Error |\n|---:|---|---|---:|---|\n";foreach($rows as $i=>$r){$md.='| '.($i+1).' | '.$r[0].' | '.($r[1]?'PASS':'FAIL').' | '.$r[2].' | '.str_replace(['|',"\n","\r"],['/',' ',' '],$r[3])." |\n";} $file=__DIR__.'/sandbox_extra_report_'.$stamp.'.md';file_put_contents($file,$md);echo $md."\nREPORT={$file}\n";