<?php

namespace App\Http\Controllers\Game;


use App\Game\GlobalUserInfo;
use App\Game\Services\BetbyTestService;
use App\Game\Services\OuroGameService;
use App\Game\Services\PlatformService;
use App\Http\helper\NumConfig;
use App\Notification\TelegramBot;
use App\Util;
use App\Utility\SetNXLock;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Game\BetBy\TransactionItem;
use App\Game\BetBy\BetSlipItem;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class BetbyTestController extends Controller
{
    protected $betbyService;

    public function __construct(BetbyTestService $betbyService)
    {
        $this->betbyService = $betbyService;
    }

    /**
     * Ping接口
     * 该方法用于检查服务的连通性。它返回当前服务器时间戳。
     *
     * @return JsonResponse
     */
    public function ping()
    {
        return response()->json(['timestamp' => time()]);
    }

    private function telLog($arg)
    {
        $this->Log($arg);
//        (new TelegramBot())->sendMsgWithEnv(json_encode($arg));
    }

    private function Log($arg)
    {
        Util::WriteLog('betby', $arg);
    }

    private function decode(Request $request)
    {
        try {
            $data = $this->betbyService->decodePayload($request->payload);
        } catch (\Exception $e){
            return false;
        }


        if ($data['aud'] != $this->betbyService->brandId || $data['iss'] != $this->betbyService->operatorId) {
            return false;
        }
        return Util::objectToArray($data['payload']);
    }
    private  $errors = ['1005' => ['code' => 1005, 'message' => 'Player is blocked'],
                   '1006' => ['code' => 1006, 'message' => 'Player not found'],
                   '1007' => ['code' => 1007, 'message' => 'Session is expired'],
                   '2001' => ['code' => 2001, 'message' => 'Not enough money'],
                   '2002' => ['code' => 2002, 'message' => 'Invalid currency'],
                   '2004' => ['code' => 2004, 'message' => 'Bad request'],
                   '2005' => ['code' => 2005, 'message' => 'Invalid JWT token'],
                   '24'   => ['code' => 24, 'message' => 'Message'],
                   '3001' => ['code' => 3001, 'message' => 'Bonus not found'],
                   '4001' => ['code' => 4001, 'message' => 'Player limits exceeded'],
                   '4002' => ['code' => 4002, 'message' => 'Maximum bonus bet limit exceeded']];

    private function getError($code)
    {
        Util::WriteLog('betbyerr', $code);
        return response()->json($this->errors[$code], 400);
    }
    /**
     * Bet Make接口
     * 该方法用于处理用户下注请求。
     *
     * 请求参数:
     * - `amount` (integer): 金额
     * - `currency` (string): 货币
     * - `player_id` (string): 玩家ID
     * - `session_id` (string): 会话ID
     * - `bonus_id` (string, 可选): 奖金ID
     * - `bonus_type` (string, 可选): 奖金类型
     * - `transaction` (array): 交易信息
     * - `betslip` (array): 投注信息
     * - `potential_win` (integer): 潜在赢利金额
     * - `potential_comboboost_win` (integer, 可选): 潜在奖金赢利金额
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function betMake(Request $request)
    {
        //{"data":{"iat":1721234686,"exp":1721234986,"jti":"d245e3d473ba4393a4bbca47de0aa117","iss":"2424946715804176387","aud":"2424948766365847552","payload":{"amount":100,"currency":"BRL","player_id":"917c74999c-b53b-eb1a-0004478930","session_id":"1315946882","transaction":{"id":"2425239357104460126","betslip_id":"2425239357104460126","player_id":"2425378291990007812","operator_id":"2424946715804176387","operator_brand_id":"2424948766365847552","ext_player_id":"917c74999c-b53b-eb1a-0004478930","timestamp":1721234686.7508757,"amount":100,"currency":"BRL","operation":"bet","cross_rate_euro":"0.167429"},"betslip":{"id":"2425239357104460126","timestamp":1721234686.739,"player_id":"2425378291990007812","operator_id":"2424946715804176387","operator_brand_id":"2424948766365847552","ext_player_id":"917c74999c-b53b-eb1a-0004478930","currency":"BRL","sum":100,"type":"1\/1","k":"2.3","is_quick_bet":false,"bets":[{"id":"2425239357104460127","event_id":"2424813655611805741","sport_id":"1","tournament_id":"2291720548469837854","category_id":"1666080098480164864","live":true,"sport_name":"Soccer","category_name":"Mexico","tournament_name":"U23 Liga MX","competitor_name":["Necaxa U23","Rayados de Monterrey U23"],"market_name":"Total","outcome_name":"over 9","scheduled":1721228400,"odds":"2.3"}],"accept_odds_change":true},"potential_win":230,"potential_comboboost_win":0},"nbf":1721234681}}
        $data = $this->decode($request);
        if(!$data)return $this->getError(2005);
        if(!isset($data['transaction'])){
            return $this->getError(2004);
        }
        $transaction = new TransactionItem($data['transaction']);
        $betslip = new BetSlipItem($data['betslip']);

        //TODO: 我们开始扩第二个站的时候，这里要进行分片逻辑了。。。要根据player_id通知其他服务器
        $user = GlobalUserInfo::getGameUserInfo('GlobalUID', $data['player_id']);

        $this->telLog(compact('user', 'data', 'betslip'));

        // TODO: 在此处实现具体的下注逻辑
        //未找到用户
        if(!$user)return $this->getError(1006);
        //货币不对
        if($data['currency']!='BRL')return $this->getError(2002);

        if (Redis::exists('discard_'.$transaction->id)) {
            return $this->getError(2004);
        }

        $res = SetNXLock::getExclusiveLock('betby_bet_'.$transaction->id, 86400);
        if (!$res) {
            return $this->getError(2004);
        }

        $score = GlobalUserInfo::getScoreByUserID($user->UserID);
        $score = $score*NumConfig::NUM_VALUE;

        $userScoreKey = 'userScore-bet-'.$user->UserID;

        if(Redis::exists($userScoreKey)){
            $score=Redis::get($userScoreKey);
        }else{

            $userScoreKey = 'userScore-bet-'.$user->UserID;
            Redis::set($userScoreKey,$score);
        }

        //没钱
        if ($score < $transaction->amount) return $this->getError(2001);

        $userScoreKey = 'userScore-bet-'.$user->UserID;
        Redis::set($userScoreKey,$score-$data['amount']);

        DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $user->UserID)->decrement('Score', $data['amount']);

        OuroGameService::notifyWebHall($user->UserID,"","pay_finish",["Golds"=>$score-$data['amount'],"PayNum"=>-$data['amount'],"event"=>"pay"]);



        //用户cancel的时候
        Redis::set('bet_amount_'.$transaction->id,$data['amount']?:1);
        Redis::expire('bet_amount_'.$transaction->id,86400*31);

        PlatformService::platformBet('betby','',$data['amount'],$user->UserID);


        return response()->json([
            'id'                    => $transaction->id,//Unique identifier for transaction assigned by Partner 我们要自己生成，可以用数据库id吧
            'ext_transaction_id'    => $transaction->id,
            'parent_transaction_id' => null,//make是一切的开始，没有parent
            'user_id'               => $data['player_id'],
            'operation'             => 'bet',
            'amount'                => $data['amount'],
            'currency'              => $data['currency'],
            'balance'               => $score-$data['amount'], // 示例余额
            'operator_bonus_amount' => 0 // 示例奖金金额
        ]);
    }

    /**
     * Bet Commit接口
     * 该方法用于确认下注交易。
     *
     * 请求参数:
     * - `bet_transaction_id` (string): 交易ID
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function betCommit(Request $request)
    {
        $data = $this->decode($request);
        $this->telLog(compact('data'));
        // TODO: 在此处实现具体的确认逻辑

        return response()->json();
    }

    /**
     * Bet Settlement接口
     * 该方法用于结算下注交易。
     *
     * 请求参数:
     * - `bet_transaction_id` (string): 交易ID
     * - `status` (string): 状态
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function betSettlement(Request $request)
    {
        $data = $this->decode($request);
        $this->telLog(compact('data'));
        // TODO: 在此处实现具体的结算逻辑

        $betLid = @$data['bet_transaction_id'];
        Redis::del('bet_amount_'.$betLid);

        return response()->json();
    }

    /**
     * Bet Refund接口
     * 该方法用于处理退款请求。
     *
     * 请求参数:
     * - `bet_transaction_id` (string): 交易ID
     * - `reason` (string): 退款原因
     * - `bonus_id` (string, 可选): 奖金ID
     * - `transaction` (array): 交易信息
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function betRefund(Request $request)
    {
        $data = $this->decode($request);

        $transaction = new TransactionItem($data['transaction']);
        $this->telLog(compact('data'));
        $user = GlobalUserInfo::getGameUserInfo('GlobalUID', $transaction->ext_player_id);
        $bet=0;
        $key='bet_amount_'.$data['bet_transaction_id'];
        $score = GlobalUserInfo::getScoreByUserID($user->UserID);
        $score = $score*NumConfig::NUM_VALUE;


        $res = SetNXLock::getExclusiveLock('refund_betby_'.$transaction->id, 86400);
        if (!$res) {
            Util::WriteLog('betbyeee','######has refunded######_'.$transaction->id);
            return response()->json([
                'id'                    => $transaction->id,
                'ext_transaction_id'    => $transaction->id,
                'parent_transaction_id' => $transaction->parent_transaction_id,
                'user_id'               => $transaction->ext_player_id,
                'operation'             => 'refund',
                'amount'                => $transaction->amount,
                'currency'              => $transaction->currency,
                'balance'               => $score+$bet // 示例余额
            ]);



//            return $this->getError(2004);
        }


        if(Redis::exists($key)){
            $bet=$transaction->amount;
            if($bet>0){
                DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $user->UserID)->increment('Score', $bet);
                PlatformService::platformBet('betby','',-$bet,$user->UserID);

                $userScoreKey = 'userScore-bet-'.$user->UserID;
                Redis::set($userScoreKey,$score+$bet);

                OuroGameService::notifyWebHall($user->UserID,"","pay_finish",["Golds"=>$score+$bet,"PayNum"=>$bet,"event"=>"pay"]);

                Util::WriteLog('betrefund', ['before_score'=>$score,'after_score'=>$score+$bet ,'data' => $data]);
            }
            //Redis::del($key);
        }


        return response()->json([
            'id'                    => $transaction->id,
            'ext_transaction_id'    => $transaction->id,
            'parent_transaction_id' => $transaction->parent_transaction_id,
            'user_id'               => $transaction->ext_player_id,
            'operation'             => 'refund',
            'amount'                => $transaction->amount,
            'currency'              => $transaction->currency,
            'balance'               => $score+$bet // 示例余额
        ]);
    }

    /**
     * Bet Win接口
     * 该方法用于处理用户赢取奖金的请求。
     *
     * 请求参数:
     * - `amount` (integer): 金额
     * - `currency` (string): 货币
     * - `is_cashout` (boolean): 是否兑现
     * - `bet_transaction_id` (string): 交易ID
     * - `transaction` (array): 交易信息
     * - `is_snr_lost` (boolean, 可选): 是否为“无风险免费投注”失利
     * - `selections` (array): 投注选择项
     * - `odds` (string, 可选): 赔率
     * - `bonus_id` (string, 可选): 奖金ID
     * - `bonus_type` (string, 可选): 奖金类型
     * - `comboboost_multiplier` (string, 可选): 奖金乘数
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function betWin(Request $request)
    {
        $data = $this->decode($request);

        $transaction = new TransactionItem($data['transaction']);
        $this->telLog(compact('data', 'transaction'));
        // TODO: 在此处实现具体的赢取奖金逻辑

        $user = GlobalUserInfo::getGameUserInfo('GlobalUID', $transaction->ext_player_id);

        Util::WriteLog('betbytt',[$transaction->ext_player_id,$user]);
        $res = SetNXLock::getExclusiveLock('win_betby_'.$transaction->id, 86400);
        $score = GlobalUserInfo::getScoreByUserID($user->UserID);
        $score = $score*NumConfig::NUM_VALUE;
        $betKey = 'bet_amount_'.$transaction->parent_transaction_id;

        $win = intval($data['amount']);
        $bet =  Redis::get($betKey)?:0;

        if (!$res) {
            return response()->json([
                'id'                    => $transaction->id,
                'ext_transaction_id'    => $transaction->id,
                'parent_transaction_id' => $transaction->parent_transaction_id,
                'user_id'               => $transaction->ext_player_id,
                'operation'             => 'win',
                'amount'                => $data['amount'],
                'currency'              => $data['currency'],
                'balance'               => $score // 示例余额
            ]);
        }

        $userScoreKey = 'userScore-bet-'.$user->UserID;
        Redis::set($userScoreKey,$score+$win);
        if(true){
            PlatformService::platformWin($user->UserID,'betby','',$win,$bet,$score+$win);
            OuroGameService::notifyWebHall($user->UserID,"","pay_finish",["Golds"=>$score+$win,"PayNum"=>$win,"event"=>"pay"]);
        }

        return response()->json([
            'id'                    => $transaction->id,
            'ext_transaction_id'    => $transaction->id,
            'parent_transaction_id' => $transaction->parent_transaction_id,
            'user_id'               => $transaction->ext_player_id,
            'operation'             => 'win',
            'amount'                => $data['amount'],
            'currency'              => $data['currency'],
            'balance'               => $score+$win // 示例余额
        ]);
    }

    /**
     * Bet Lost接口
     * 该方法用于处理用户投注失败的请求。
     *
     * 请求参数:
     * - `bet_transaction_id` (string): 交易ID
     * - `amount` (integer): 金额
     * - `currency` (string): 货币
     * - `transaction` (array): 交易信息
     * - `selections` (array): 投注选择项
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function betLost(Request $request)
    {
        $data = $this->decode($request);

        $transaction = new TransactionItem($data['transaction']);
        $this->telLog(compact('data', 'transaction'));
        // TODO: 在此处实现具体的投注失败逻辑

        $user = GlobalUserInfo::getGameUserInfo('GlobalUID', $transaction->ext_player_id);
        $res = SetNXLock::getExclusiveLock('lost_betby_'.$transaction->id, 86400);
        $score = GlobalUserInfo::getScoreByUserID($user->UserID);
        $score = $score*NumConfig::NUM_VALUE;
        if (!$res) {
            //Util::WriteLog('24680_win_error_betby',$data);
            //return ['success' => false,'code' => 2401,'message' => 'Session not found or expired.'];
            return response()->json([
                'id'                    => $transaction->id,
                'ext_transaction_id'    => $transaction->id,
                'parent_transaction_id' => $transaction->parent_transaction_id,
                'user_id'               => $transaction->ext_player_id,
                'operation'             => 'lost',
                'balance'               => $score // 示例余额
            ]);
            //return $this->getError(2004);
        }

        $betKey = 'bet_amount_'.$transaction->parent_transaction_id;

        $win = 0;
        $bet =  Redis::get($betKey)?:0;
        if(true){
            PlatformService::platformWin($user->UserID,'betby','',$win,$bet,$score+$win);
        }

        return response()->json([
            'id'                    => $transaction->id,
            'ext_transaction_id'    => $transaction->id,
            'parent_transaction_id' => $transaction->parent_transaction_id,
            'user_id'               => $transaction->ext_player_id,
            'operation'             => 'lost',
            'balance'               => $score // 示例余额
        ]);
    }

    /**
     * Bet Discard接口
     * 该方法用于处理丢弃投注的请求。
     *
     * 请求参数:
     * - `ext_player_id` (string): 玩家外部ID
     * - `transaction_id` (string): 交易ID
     * - `reason` (string): 丢弃原因
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function betDiscard(Request $request)
    {
        $data = $this->decode($request);

        $this->telLog(compact('data'));

        $user = GlobalUserInfo::getGameUserInfo('GlobalUID', $data['ext_player_id']);
        $bet=0;
        $key='bet_amount_'.$data['transaction_id'];
        $score = GlobalUserInfo::getScoreByUserID($user->UserID);
        $score = $score*NumConfig::NUM_VALUE;

        $res = SetNXLock::getExclusiveLock('discard_'.$data['transaction_id'], 86400);
        if (!$res) {
            return response()->json();
            //return ['success' => false,'code' => 2401,'message' => 'Session not found or expired.'];
            //return ['success' => true,'balance' => intval($score)];
        }

        if(Redis::exists($key)){
            $bet=Redis::get($key);
            if($bet>0){
                DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $user->UserID)->increment('Score', $bet);
                PlatformService::platformBet('betby','',-$bet,$user->UserID);

                $userScoreKey = 'userScore-bet-'.$user->UserID;
                Redis::set($userScoreKey,$score+$bet);

                OuroGameService::notifyWebHall($user->UserID,"","pay_finish",["Golds"=>$score+$bet,"PayNum"=>$bet,"event"=>"pay"]);

                Util::WriteLog('betbydiscard', ['before_score'=>$score,'after_score'=>$score+$bet ]);
            }
            Redis::del($key);
        }

        return response()->json();
    }

    /**
     * Bet Rollback接口
     * 该方法用于处理回滚投注的请求。
     *
     * 请求参数:
     * - `bet_transaction_id` (string): 交易ID
     * - `parent_transaction_id` (string): 父交易ID
     * - `transaction` (array): 交易信息
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function betRollback(Request $request)
    {
        $data = $this->decode($request);

        $transaction = new TransactionItem($data['transaction']);
        $this->telLog(compact('data', 'transaction'));
        // TODO: 在此处实现具体的回滚逻辑

        $user = GlobalUserInfo::getGameUserInfo('GlobalUID', $transaction->ext_player_id);
        $score = GlobalUserInfo::getScoreByUserID($user->UserID);
        $score = $score*NumConfig::NUM_VALUE;

        $key='bet_amount_'.$data['bet_transaction_id'];
        if(!Redis::exists($key)){

            Util::WriteLog('betbyeee','######not find bet key######_'.$data['bet_transaction_id']);

            return response()->json([
                'id'                    => $transaction->id,
                'ext_transaction_id'    => $transaction->id,
                'parent_transaction_id' => $transaction->parent_transaction_id,
                'user_id'               => $transaction->ext_player_id,
                'operation'             => 'rollback',
                'amount'                => $transaction->amount,
                'currency'              => $transaction->currency,
                'balance'               => max($score,0) // 示例余额
            ]);

            //return $this->getError(2004);
        }


        $res = SetNXLock::getExclusiveLock('rollbackn_'.$transaction->id, 86400);
        if (!$res) {
            Util::WriteLog('betbyeee','######repeat rollback######_'.$data['bet_transaction_id']);
            return response()->json([
                'id'                    => $transaction->id,
                'ext_transaction_id'    => $transaction->id,
                'parent_transaction_id' => $transaction->parent_transaction_id,
                'user_id'               => $transaction->ext_player_id,
                'operation'             => 'rollback',
                'amount'                => $transaction->amount,
                'currency'              => $transaction->currency,
                'balance'               => max($score,0) // 示例余额
            ]);

            //return $this->getError(2004);
            //return ['success' => false,'code' => 2401,'message' => 'Session not found or expired.'];
            //return ['success' => true,'balance' => intval($score)];
        }




        $amount = $transaction->amount;

        if($amount>$score){
            $special = ['user_id' => $user->UserID,'current_score' => $score,'rollback_amount' => $amount];
            Util::WriteLog('betby_roollback',compact('data',$special));
            $amount = $score;
        }

        //$bet =  Redis::get($key)?:0;
        if(true){
            DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $user->UserID)->decrement('Score', $amount);
            PlatformService::platformWin($user->UserID,'betby','',-$amount,0,$score-$amount);
        }

        $userScoreKey = 'userScore-bet-'.$user->UserID;
        Redis::set($userScoreKey,max($score-$amount,0));

        return response()->json([
            'id'                    => $transaction->id,
            'ext_transaction_id'    => $transaction->id,
            'parent_transaction_id' => $transaction->parent_transaction_id,
            'user_id'               => $transaction->ext_player_id,
            'operation'             => 'rollback',
            'amount'                => $transaction->amount,
            'currency'              => $transaction->currency,
            'balance'               => max($score-$amount,0) // 示例余额
        ]);
    }
}
