<?php

namespace App\Http\logic\api;

use App\dao\Estatisticas\RechargeWithDraw;
use App\Inter\CashierInterFace;
use App\Models\PrivateMail;
use App\Models\RecordUserDataStatistics;
use App\Services\CoinPay;
use App\Services\StoredProcedure;
use App\Util;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class CoinPayCashierLogic implements CashierInterFace
{
    const AGENT = 102;

    public function payment($RecordID, $amount, $accountName, $phone, $email, $OrderId, $PixNum, $PixType, $IFSCNumber, $BranchBank, $BankNO)
    {
        $query = DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')->where('RecordID', $RecordID)->first();
        if (!$query) {
            return 'fail';
        }

        $service = new CoinPay('CoinPayOut');
        $config = $service->getConfig();

        $withdrawInfo = DB::table('QPAccountsDB.dbo.AccountWithDrawInfo')->where('UserID', $query->UserID)->first();
        $address = $PixNum ?: ($withdrawInfo->PixNum ?? $withdrawInfo->BankNO ?? $BankNO);
        if (!$address) {
            Util::WriteLog('CoinPay', 'missing wallet address for withdraw: ' . $OrderId);
            return 'fail';
        }

        $language = strtoupper($config['language'] ?? 'EN');
        $coin = strtoupper($config['coin'] ?? 'USDT');
        $protocol = strtoupper($config['protocol'] ?? 'TRC20');
        $rateType = (int)($config['rateType'] ?? 2);
        $currency = strtoupper($config['currency'] ?? 'USD');

        $amountDecimal = number_format($amount / 100, 2, '.', '');

        $params = [
            'merchantMemberNo' => (string)$query->UserID,
            'merchantOrderNo' => $OrderId,
            'language' => $language,
            'coin' => $coin,
            'rateType' => $rateType,
            'protocol' => $protocol,
            'notifyUrl' => $config['cashNotify'] ?? '',
            'toAddress' => $address,
            'timestamp' => time(),
        ];

        if ($rateType === 1) {
            $params['amount'] = number_format($amountDecimal, 8, '.', '');
            $params['rate'] = $config['rate'] ?? '1';
        } else {
            $params['currencyAmount'] = $amountDecimal;
            $params['currency'] = $currency;
        }

        $signedParams = $service->sign($params);
        $response = $service->post('/order/withdrawOrderCoinCreate', $signedParams);

        Log::info('CoinPay withdraw request', $signedParams);
        Log::info('CoinPay withdraw response', [$response]);

        try {
            $data = \GuzzleHttp\json_decode($response, true);
        } catch (\Throwable $e) {
            Util::WriteLog('CoinPay_error', $e->getMessage());
            return 'fail';
        }

        if (isset($data['code']) && (int)$data['code'] === 0) {
            return $data;
        }

        return 'fail';
    }

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

        Util::WriteLog('CoinPay', 'withdraw notify: ' . json_encode($post, JSON_UNESCAPED_UNICODE));

        $OrderId = $post['merchantOrderNo'] ?? '';
        $query = DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')->where('OrderId', $OrderId)->first();
        if (!$query) {
            Util::WriteLog('CoinPay', 'withdraw order not found');
            return '{"success":true,"message":"order not found"}';
        }

        if (!in_array($query->State, [5, 7])) {
            Util::WriteLog('CoinPay', 'withdraw already handled: ' . $OrderId);
            return 'SUCCESS';
        }

        $status = (int)($post['state'] ?? 0);
        $now = now();
        $withdraw_data = [];
        $msg = $post['msg'] ?? '';

        $TakeMoney = $query->WithDraw + $query->ServiceFee;
        $UserID = $query->UserID;

        switch ($status) {
            case 3:
                $withdraw_data = [
                    'State' => 2,
                    'agent' => self::AGENT,
                    'finishDate' => $now,
                ];
                $this->handleSuccess($UserID, $TakeMoney, $OrderId, $query->ServiceFee);
                break;

            case 4:
            case 5:
            case 6:
            case 7:
                $msg = $msg ?: 'Withdraw rejected';
                $bonus = '30000,' . $TakeMoney;
                PrivateMail::failMail($UserID, $OrderId, $TakeMoney, $msg, $bonus);

                $withdraw_data = [
                    'State' => 6,
                    'agent' => self::AGENT,
                    'remark' => $msg,
                ];
                break;

            default:
                return 'SUCCESS';
        }

        $recordData = [
            'before_state' => $query->State,
            'after_state' => $withdraw_data['State'] ?? $query->State,
            'RecordID' => $query->RecordID,
            'update_at' => date('Y-m-d H:i:s'),
        ];

        DB::connection('write')->table('QPAccountsDB.dbo.AccountsRecord')
            ->updateOrInsert(['RecordID' => $query->RecordID, 'type' => 1], $recordData);
        DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')
            ->where('OrderId', $OrderId)
            ->update($withdraw_data);

        return 'SUCCESS';
    }

    protected function handleSuccess($UserID, $TakeMoney, $OrderId, $serviceFee): void
    {
        $first = DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->where('UserID', $UserID)->first();
        if ($first) {
            DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->where('UserID', $UserID)->increment('TakeMoney', $TakeMoney);
        } else {
            DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->insert(['TakeMoney' => $TakeMoney, 'UserID' => $UserID]);
            try {
                PrivateMail::praiseSendMail($UserID);
            } catch (\Throwable $e) {
            }
        }

        try {
            StoredProcedure::addPlatformData($UserID, 4, $TakeMoney);
        } catch (\Throwable $exception) {
            Util::WriteLog('StoredProcedure', $exception->getMessage());
        }

        $withdrawal_position_log = DB::connection('write')->table('agent.dbo.withdrawal_position_log')->where('order_sn', $OrderId)->first();
        if ($withdrawal_position_log) {
            DB::connection('write')->table('agent.dbo.withdrawal_position_log')
                ->where('order_sn', $OrderId)
                ->update(['take_effect' => 2, 'update_at' => date('Y-m-d H:i:s')]);
        }

        RecordUserDataStatistics::updateOrAdd($UserID, $TakeMoney, 0, $serviceFee);
        $fee = DB::table('QPAccountsDB.dbo.OrderWithDraw')->where('OrderId', $OrderId)
            ->value('withdraw_fee');
        (new RechargeWithDraw())->withDraw($UserID, $TakeMoney, $fee, $serviceFee);
        $redis = Redis::connection();
        $redis->incr('draw_' . date('Ymd') . $UserID);
        PrivateMail::successMail($UserID, $OrderId, $TakeMoney);

        StoredProcedure::user_label($UserID, 2, $TakeMoney);
    }
}

