<?php

namespace App\Http\logic\api;

use App\dao\Pay\AccountPayInfo;
use App\dao\Pay\PayController;
use App\Facade\TableName;
use App\Http\helper\CreateOrder;
use App\Http\helper\NumConfig;
use App\Jobs\Order;
use App\Notification\TelegramBot;
use App\Services\OrderServices;
use App\Services\PayConfig;
use App\Services\WDPay;
use App\Services\CreateLog;
use App\Util;
use Illuminate\Support\Facades\DB;

class WDPayLogic extends BaseApiLogic
{
    public $result;

    public function pay_order($userId, $pay_amount, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
    {
        $dao = new AccountPayInfo();
        [$userPhone, $userName, $userEmail] = $dao->payInfo($userId);
        
        // 礼包类型验证
        $PayVerify = new PayController();
        $pay_amount = $PayVerify->verify($userId, $GiftsID, $pay_amount);
        if ($PayVerify->verify($userId, $GiftsID, $pay_amount) === false) {
            $this->error = $PayVerify->getError();
            return false;
        }
        if ($pay_amount < 0) {
            $this->error = 'Payment error_4';
            return false;
        }

        $service = new WDPay();
        $config = $service->config;

        $order_sn = CreateOrder::order_sn($userId);

        // 生成订单信息（WDPay使用元，但数据库存储还是用分）
        $logic = new OrderLogic();
        $amount = (int) round($pay_amount * NumConfig::NUM_VALUE);
        $logic->orderCreate($order_sn, $amount, 'WDPay', $userId, $pay_method, $GiftsID, $AdId, $eventType);

        $payMethods = [
            1 => 'cashapp',
            2 => 'paypal',
            4 => 'applepay',
            8 => 'googlepay'
        ];

        $wdMethod = @$payMethods[$pay_method];

        // 构建支付请求参数（按WDPay官方文档 /charging/create-pay-in）
        // 金额格式：元，必须保留2位小数，如 4.99
        $params = [
            "country" => "US",  // 国家，默认US
            "currency" => "USD",  // 货币，默认USD
            "amount" => number_format($pay_amount, 2, '.', ''),  // 价格，必须保留2位小数
            "platform" =>Util::getDeviceType(),  // 设备：android/ios
            "ip" => $buyIP,  // 用户真实IP
            "username" => 'user' . $userId,  // 用户名称
            "customer" => $config['customer'],  // 商户简称
            "customerNo" => $config['customerNo'],  // 商户编号
            "customerOrderNo" => $order_sn,  // 商户自定义订单号
            "wayCode" => $wdMethod ? $wdMethod : ($config['wayCode'] ?? "cashapp"),  // cashapp或paypal
            "timestamp" => (string)round(microtime(true) * 1000),  // 13位时间戳（毫秒）
            "signType" => "MD5",  // 传MD5
            "callback" => $config['notify'] ?? '',  // 异步通知地址
        ];
        
        // redirectUrl 是可选参数
        if (!empty($config['return'])) {
            $params["redirectUrl"] = $config['return'];  // 支付成功后跳转的页面
        }

        // 签名
        $signedParams = $service->sign($params);

        // 生成用户请求信息日志
        $request_extra = \GuzzleHttp\json_encode($signedParams);
        CreateLog::pay_request($userPhone, $request_extra, $order_sn, $userEmail, $userId, $userName);

        $url = $service->apiUrl;

        $result = $service->curlPost($url, $signedParams);
        $rresult = compact('result', 'signedParams', 'url');
        
        Util::WriteLog('WDPay', 'WDPay支付请求' . $url . " | " . $request_extra);
        Util::WriteLog('WDPay', 'WDPay支付结果' . json_encode($rresult));
        
        try {
            $res = \GuzzleHttp\json_decode($result, true);
        } catch (\Exception $e) {
            Util::WriteLog("WDPay_error", [$result, $e->getMessage(), $e->getTraceAsString()]);
            $this->error = 'Payment processing failed';
            return false;
        }

        // WDPay官方返回格式：
        // {"code": 200, "data": {...}, "message": "success"}
        if (isset($res['code']) && $res['code'] == 200) {
            // 转换为统一格式供控制器使用
            return [
                'code' => 0,  // 统一成功码
                'data' => [
                    'cashierUrl' => $res['data']['paylink'] ?? '',  // 支付链接
                    'mchOrderNo' => $order_sn,  // 商户订单号
                    'orderNo' => $res['data']['id'] ?? '',  // WDPay订单号
                    'amount' => $res['data']['amount'] ?? $pay_amount,
                    'currency' => $res['data']['currency'] ?? 'USD',
                    'status' => $res['data']['status'] ?? 'initiated',
                    'paychannel' => $res['data']['paychannel'] ?? '',
                    'time_created' => $res['data']['time_created'] ?? time(),
                ]
            ];
        } else {
            $this->error = $res['message'] ?? $res['msg'] ?? 'Payment failed';
            Util::WriteLog('WDPay_error', 'Payment failed: ' . json_encode($res));
            return false;
        }
    }

    public function notify($post)
    {
        try {
            if (!is_array($post)) $post = \GuzzleHttp\json_decode($post, true);

            Util::WriteLog('WDPay', "WDPay异步回调处理\n" . json_encode($post, JSON_UNESCAPED_UNICODE));

            // WDPay官方回调字段：
            // customerOrderNo: 商户订单号
            // orderNo: WDPay订单号
            // amount: 金额（元）
            // currency: 货币
            // status: 订单状态 (succeeded=成功, closed=失败)
            // signType: MD5
            // sign: 签名
            
            $order_sn = $post['customerOrderNo'] ?? '';  // 商户订单号

            $order = DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)->first();

            if (!$order) {
                Util::WriteLog('WDPay', '订单不存在: ' . $order_sn);
                return response()->json(['success' => false, 'message' => 'Order not found']);
            }

            // 订单已处理，直接返回成功
            if ((!empty($order->pay_at) || !empty($order->finished_at))) {
                Util::WriteLog('WDPay', '订单已处理: ' . $order_sn);
//                return 'success';
                return '{"msg":"success","code":200}';
            }

            $body = [
                'payment_sn' => $post['orderNo'] ?? '',  // WDPay订单号
                'updated_at' => date('Y-m-d H:i:s'),
            ];

            $ordStatus = $post['status'] ?? '';  // WDPay状态字段
            $GiftsID = $order->GiftsID ?: '';
            $userID = $order->user_id ?: '';
            $AdId = $order->AdId ?: '';
            $eventType = $order->eventType ?: '';
            // WDPay金额是元，需要转换为分
            $payAmt = floatval($post['amount'] ?? 0);

            // WDPay状态判断：succeeded=成功, closed=失败
            switch (strtolower($ordStatus)) {
                case 'succeeded':  // 支付成功（官方状态）

                    $body['pay_status'] = 1;
                    $body['pay_at'] = date('Y-m-d H:i:s');
                    $body['finished_at'] = date('Y-m-d H:i:s');
                    $body['amount'] = (int) round($payAmt * NumConfig::NUM_VALUE);
                    $config = (new PayConfig())->getConfig('WDPay');
                    $payRates = @$config['pay_rate'];
                    if(is_array($payRates)){
                        $payMethod = $order->order_title??1;
                        $payRate = $payRates[$payMethod] ?? $payRates[1];
                        $body['payment_fee'] = intval(($body['amount'] * ($payRate[0] ?? 15))/100)+($payRate[1]??0.3)*NumConfig::NUM_VALUE;
                    }
                    
                    try {
                        // 获取金额
                        $service = new OrderServices();

                        if (intval($order->amount) != $body['amount']) {
                            // 金额不匹配，按实际支付金额处理
                            $body['GiftsID'] = 0;
                            $body['amount'] = (int) round($payAmt * NumConfig::NUM_VALUE);
                            $Recharge = $payAmt;
                            $give = 0;
                            $favorable_price = $Recharge + $give;
                            $czReason = 1;
                            $cjReason = 45;
                        } else {
                            [$give, $favorable_price, $Recharge, $czReason, $cjReason] = $service->getPayInfo($GiftsID, $userID, $payAmt);
                        }

                        // 增加充值记录
                        [$Score] = $service->addRecord($userID, $payAmt, $favorable_price, $order_sn, $GiftsID, $Recharge, $czReason, $give, $cjReason, $AdId, $eventType, $body['payment_fee'] ?? 0);
                        // 成功处理回调
                        Order::dispatch([$userID, $payAmt, $Score, $favorable_price, $GiftsID, $order_sn]);

                        Util::WriteLog("WDPay", "订单处理成功: {$order_sn}, 用户: {$userID}, 金额: {$payAmt}, 到账: {$Score}");

                    } catch (\Exception $exception) {
                        Util::WriteLog("WDPay_error", "订单处理异常: " . $exception->getMessage());
                        throw $exception;
                    }

                    break;
                    
                case 'closed':    // 支付失败（官方状态）
                    $body['pay_status'] = 2;
                    Util::WriteLog('WDPay', "订单支付失败: {$order_sn}, 状态: closed");
                    break;
                    
                default:          // 其他状态（不应该出现）
                    Util::WriteLog('WDPay', "订单未知状态: {$order_sn}, 状态: {$ordStatus}");
                    return '{"msg":"success","code":200}';  // 返回成功
            }

            $order_up = DB::connection('write')->table('agent.dbo.order')
                ->where('order_sn', $order_sn)
                ->update($body);

            if (!$order_up) {
                Util::WriteLog('WDPay', '订单更新失败: ' . $order_sn);
                return response()->json(['success' => false, 'message' => 'Update failed']);
            }

            Util::WriteLog("WDPay", "订单回调处理完成: {$order_sn}");

            return '{"msg":"success","code":200}';

        } catch (\Exception $exception) {
            Util::WriteLog("WDPay_error", "回调异常: " . $exception->getMessage() . "\n" . $exception->getTraceAsString());
            throw $exception;
        }
    }
}
