<?php

namespace app\api\controller;

use app\admin\model\Game;
use app\admin\model\PromoReward;
use app\admin\model\Provider;
use app\common\controller\Api;
use app\common\model\Config;
use app\common\service\JK8Services;
use app\common\model\User;

class External extends Api
{
    protected $jk8Services;
    public function initialize(): void
    {
        parent::initialize();
        $this->jk8Services = app(JK8Services::class);
    }

    public function depositNotify()
    {
        $appSecret = config('xyx')['app_secret'];
        $appSecret = '4a01e8dfaa93b69723e937c04962af26';
        $expireSeconds = 300;
        $requestParams = $this->request->param();

        if (!isset($requestParams['sign']) || !isset($requestParams['timestamp'])) {
            $this->success(__('Missing sign or timestamp'));
        }

        $currentTime = time();
        if (abs($currentTime - intval($requestParams['timestamp'])) > $expireSeconds) {
            $this->success(__('Request has expired'));
        }

        $receivedSign = $requestParams['sign'];
        unset($requestParams['sign']);

        ksort($requestParams);

        $stringToBeSigned = http_build_query($requestParams) . '&app_secret=' . $appSecret;

        $calculatedSign = hash('sha256', $stringToBeSigned);

        if (!hash_equals($calculatedSign, $receivedSign)) {
            return json(['status'=> 'FAIL']);
        }
        $user = User::where('xyx_account', $requestParams['account'])->find();
//        $transactionId = $this->jk8Services->setScore($user['jk_username'], $requestParams['prize_amount']);
        $promoReward = new PromoReward;
        $data = [
            'user_id' => $user['id'],
            'game_type' => $requestParams['game_id'],
            'amount' => $requestParams['prize_amount'],
        ];
        $promoReward->save($data);
        return json(['status'=> 'OK']);
    }

    public function claimPrizeNotify()
    {
        $config = Config::whereIn('name', ['app_id', 'app_secret'])->where('group', 'basics')->column('value', 'name');
        $appSecret = $config['app_secret'];
        $expireSeconds = 300;
        $requestParams = $this->request->param();

        if (!isset($requestParams['sign']) || !isset($requestParams['timestamp'])) {
            $this->success(__('Missing sign or timestamp'));
        }

        $currentTime = time();
        if (abs($currentTime - intval($requestParams['timestamp'])) > $expireSeconds) {
            $this->success(__('Request has expired'));
        }

        $receivedSign = $requestParams['sign'];
        unset($requestParams['sign']);


        ksort($requestParams);

        $stringToBeSigned = http_build_query($requestParams) . '&app_secret=' . $appSecret;

        $calculatedSign = hash('sha256', $stringToBeSigned);

        if (!hash_equals($calculatedSign, $receivedSign)) {
            return json(['status'=> 'FAIL']);
        }
        $user = User::where('xyx_account', $requestParams['account'])->find();
//        $transactionId = $this->jk8Services->setScore($user['jk_username'], $requestParams['prize_amount']);
        $promoReward = new PromoReward;
        $data = [
            'user_id' => $user['id'],
            'game_type' => $requestParams['game_id'],
            'amount' => $requestParams['prize_amount'],
        ];
        $promoReward->save($data);
        return json(['status'=> 'OK']);
    }

    public function gameRtp()
    {
        $provider = $this->request->param('provider');
        $limit = request()->param('limit/d') ?? 15;

        $where = [];
        if (!empty($provider)) {
            $where[] = ['provider_site', '=', $provider];
        }
        $res = Game::where($where)
            ->order('id', 'desc')
            ->paginate($limit);

        $this->success('', [
            'provider' => Provider::column('site'),
            'list'   => $res->items(),
            'total'  => $res->total(),
        ]);
    }
}