<?php
declare(strict_types=1);
$_SERVER['HTTP_HOST']='core.scan-pay.invalid';$_SERVER['HTTP_PLATFORM']='POC';$_SERVER['REQUEST_METHOD']='GET';$_SERVER['REQUEST_URI']='/api/mealCheckout/directPay';$_SERVER['SCRIPT_NAME']='/index.php';
require __DIR__.'/app.php';ck(!file_exists($runtime.'/guard-attempt.json'),'Original guard once');saveOnce('guard-attempt.json',['lane'=>$lane,'at'=>date('c')]);
try{$app=coreApp('GUARD_002');require_once $root.'/app/api/common.php';$ids=CoreGate::$ids;$fresh=json_decode(file_get_contents($runtime.'/fresh-preflight.json'),true);$before=$fresh['own'];$old=$fresh['old_protection'];
 $token='CDXDIRECT002_'.$lane.'_U1_'.bin2hex(random_bytes(24));$tokens=['U1'=>$token];saveOnce('tokens.json',$tokens);ck(\think\facade\Cache::set($token,['user'=>['user_id'=>$ids['AI_U1']],'password_version'=>0,'login_type'=>'thirdparty'],300),'Original one own FileCache token');
 $u=\app\api\model\User::getUserByToken($token);ck($u&&(int)$u['user_id']===$ids['AI_U1']&&(int)$u['store_id']===2147001111,'Original API User token/profile tenant');$controller=new \app\api\controller\MealCheckout($app);
 $g=new \app\common\service\onecard\UserGuard();$guardAi=$g->isOneCardUserByAiUserId($ids['AI_U1']);$guardZhct=$g->isOneCardUserByZhctIdentity($ids['STORE_U1'],$before['STORE_S1'][0]['uuid']);ck($guardAi===false&&$guardZhct===false,'Original actual OneCard guards false');
 $balances=(new \app\common\service\account\AccountSettlementService())->getAvailableBalance($ids['AI_U1'],date('Y-m-d H:i:s'));$calc=(new \app\common\service\meal\MarketingStrategyCalculator())->applyByAiUserId($ids['AI_U1'],'0.02',date('Y-m-d H:i:s'),$ids['STORE_R1'],4,$ids['STORE_U1'],1);$limit=(new \app\common\service\consumeLimit\ConsumeLimitEvaluator())->assertAllowed($ids['AI_U1'],$ids['STORE_R1'],1,date('Y-m-d H:i:s'),'direct_pay','0.02');
 ck($limit['allowed']===true&&$limit['restricted_account_types']===[],'Original consume-limit no-match');ck($calc['marketing_strategy_id']===0&&$calc['pay_price']==='0.02','Original marketing no-match');
 $order=readNative('ZHCT','SELECT config_value FROM ydy_config WHERE config_key=? AND restaurant_id=? AND status=1',['account_deduction_order',1],true);$sequence=json_decode($order[0]['config_value']??'',true);ck($sequence===['gift','subsidy','cash'],'Exact deduction order');
 ck(($balances['subsidy']??null)==='0.00'&&($balances['gift']??null)==='0.00','Effective excluded subsidy/gift0');
 saveOnce('guard.json',['lane'=>$lane,'original_Auth_U1'=>true,'original_API_Controller_store_check'=>true,'Guard_AI'=>$guardAi,'Guard_StoreIdentity'=>$guardZhct,'balances'=>$balances,'marketing'=>$calc,'consume_limit'=>$limit,'deduction_order'=>$sequence,'own_fullrows'=>$before,'old_protection'=>$old,'redis_branch'=>'Original own FileCache fallback only','business_native_SQL'=>CoreGate::$total,'business_DML'=>0,'HTTP'=>0,'at'=>date('c')]);echo json_encode(['lane'=>$lane,'original_auth1'=>true,'guard_false'=>true,'business_native_SQL'=>CoreGate::$total,'HTTP'=>0,'DML'=>0]),"\n";
}catch(Throwable $e){saveOnce('guard-failed.json',['class'=>get_class($e),'message'=>$e->getMessage(),'file'=>$e->getFile(),'line'=>$e->getLine()]);throw $e;}
