<?php

namespace App\Http\Controllers\admin;

use App\Constants\HttpStatus;
use App\Http\Controllers\Controller;
use App\Models\PaymentOrder;
use App\Models\Recharge;
use App\Models\User;
use App\Models\BalanceLog;
use Illuminate\Validation\ValidationException;
use Exception;
use Carbon\Carbon;
use App\Models\Withdraw;

class Home extends Controller
{
    public function index()
    {

        try {
            request()->validate([
                'start_date' => ['nullable', 'date_format:Y-m-d'],
                'end_date' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:start_date'],
            ]);

            $start = request()->input('start_date');
            $end = request()->input('end_date');
            if (empty($end)) $end = date('Y-m-d');
            if (empty($start)) $start = date('Y-m-d');
            $end = Carbon::createFromFormat('Y-m-d', $end)->addDay()->format('Y-m-d');
            $today = date('Y-m-d');

            //投注统计
            $query = BalanceLog::leftJoin('users', 'users.member_id', '=', 'balance_logs.member_id');
            $query->where('balance_logs.type', 1)->where('balance_logs.change_type', 'like', '%投注')->where('users.from', '<>', 2);
            $query1 = clone $query;
            $bet['amount'] = (float)$query1->whereBetween('balance_logs.created_at', [$start, $end])->sum('amount');//投注金额
            $bet['amount'] = abs($bet['amount']);
            $query1 = clone $query;
            $bet['today_amount'] = (float)$query1->whereBetween('balance_logs.created_at', [$today, $end])->sum('amount');//今日投注金额
            $bet['today_amount'] = abs($bet['today_amount']);

            //投注统计
            $query = BalanceLog::leftJoin('users', 'users.member_id', '=', 'balance_logs.member_id');
            $query->where('balance_logs.type', 1)->where('balance_logs.change_type', 'like', '%投注')->where('users.from', 2);
            $query1 = clone $query;
            $virtual_bet['amount'] = (float)$query1->whereBetween('balance_logs.created_at', [$start, $end])->sum('amount');//投注金额
            $virtual_bet['amount'] = abs($virtual_bet['amount']);
            $query1 = clone $query;
            $virtual_bet['today_amount'] = (float)$query1->whereBetween('balance_logs.created_at', [$today, $end])->sum('amount');//今日投注金额
            $virtual_bet['today_amount'] = abs($virtual_bet['today_amount']);

            //投注退款统计
            $query = BalanceLog::leftJoin('users', 'users.member_id', '=', 'balance_logs.member_id');
            $query->where('balance_logs.type', 1)->where('balance_logs.change_type', 'like', '%退款')->where('users.from', '<>', 2);
            $query1 = clone $query;
            $bet['refund_amount'] = (float)$query1->whereBetween('balance_logs.created_at', [$start, $end])->sum('amount');//投注退款金额
            $query1 = clone $query;
            $bet['today_refund_amount'] = (float)$query1->whereBetween('balance_logs.created_at', [$today, $end])->sum('amount');//今日投注退款金额

            //投注退款统计
            $query = BalanceLog::leftJoin('users', 'users.member_id', '=', 'balance_logs.member_id');
            $query->where('balance_logs.type', 1)->where('balance_logs.change_type', 'like', '%退款')->where('users.from', 2);
            $query1 = clone $query;
            $virtual_bet['refund_amount'] = (float)$query1->whereBetween('balance_logs.created_at', [$start, $end])->sum('amount');//投注退款金额
            $query1 = clone $query;
            $virtual_bet['today_refund_amount'] = (float)$query1->whereBetween('balance_logs.created_at', [$today, $end])->sum('amount');//今日投注退款金额

            //投注中奖统计
            $query = BalanceLog::leftJoin('users', 'users.member_id', '=', 'balance_logs.member_id');
            $query->where('balance_logs.type', 1)->where('balance_logs.change_type', 'like', '%中奖')->where('users.from', '<>', 2);
            $query1 = clone $query;
            $bet['win_amount'] = (float)$query1->whereBetween('balance_logs.created_at', [$start, $end])->sum('amount');//投注中奖金额
            $query1 = clone $query;
            $bet['today_win_amount'] = (float)$query1->whereBetween('balance_logs.created_at', [$today, $end])->sum('amount');//今日投注中奖金额

            //投注中奖统计
            $query = BalanceLog::leftJoin('users', 'users.member_id', '=', 'balance_logs.member_id');
            $query->where('balance_logs.type', 1)->where('balance_logs.change_type', 'like', '%中奖')->where('users.from', 2);
            $query1 = clone $query;
            $virtual_bet['win_amount'] = (float)$query1->whereBetween('balance_logs.created_at', [$start, $end])->sum('amount');//投注中奖金额
            $query1 = clone $query;
            $virtual_bet['today_win_amount'] = (float)$query1->whereBetween('balance_logs.created_at', [$today, $end])->sum('amount');//今日投注中奖金额

            //余额宝统计
            $query = BalanceLog::query();
            $yuebao['in_amount'] = (float)$query->where('change_type', '余额宝转入')->whereBetween('created_at', [$start, $end])->sum('amount');//余额宝转入金额
            $yuebao['in_amount'] = abs($yuebao['in_amount']);
            $yuebao['today_in_amount'] = (float)$query->where('change_type', '余额宝转入')->whereBetween('created_at', [$today, $end])->sum('amount');//今日余额宝转入金额
            $yuebao['today_in_amount'] = abs($yuebao['today_in_amount']);

            $yuebao['out_amount'] = (float)$query->where('change_type', '余额宝转出')->whereBetween('created_at', [$start, $end])->sum('amount');//余额宝转出金额
            $yuebao['out_amount'] = abs($yuebao['out_amount']);
            $yuebao['today_out_amount'] = (float)$query->where('change_type', '余额宝转出')->whereBetween('created_at', [$today, $end])->sum('amount');//今日余额宝转出金额
            $yuebao['today_out_amount'] = abs($yuebao['today_out_amount']);
            
            $yuebao['interest_amount'] = (float)$query->where('change_type', '余额宝利息')->whereBetween('created_at', [$start, $end])->sum('amount');//余额宝利息
            $yuebao['today_interest_amount'] = (float)$query->where('change_type', '余额宝利息')->whereBetween('created_at', [$today, $end])->sum('amount');//今日余额宝利息

            //老用户回归返彩金额
            $activity['user_return'] = (float)$query->where('change_type', '老用户回归')->whereBetween('created_at', [$start, $end])->sum('amount');//老用户回归返彩金额
            $activity['today_user_return'] = (float)$query->where('change_type', '老用户回归')->whereBetween('created_at', [$today, $end])->sum('amount');//今日老用户回归返彩金额

            //即充即送返彩金额
            $activity['recharge_frozen'] = (float)$query->where('change_type', '即充即送')->whereBetween('created_at', [$start, $end])->sum('amount');//即充即送返彩金额
            $activity['today_recharge_frozen'] = (float)$query->where('change_type', '即充即送')->whereBetween('created_at', [$today, $end])->sum('amount');//今日即充即送返彩金额

            //充值返现金额
            $activity['recharge_amount'] = (float)$query->where('change_type', '充值返现')->whereBetween('created_at', [$start, $end])->sum('amount');//充值返现金额
            $activity['today_recharge_amount'] = (float)$query->where('change_type', '充值返现')->whereBetween('created_at', [$today, $end])->sum('amount');//今日充值返现金额

            //流水解冻金额
            $activity['unfreeze_amount'] = (float)$query->where('change_type', '流水解冻')->whereBetween('created_at', [$start, $end])->sum('amount');//流水解冻金额
            $activity['unfreeze_amount'] = abs($activity['unfreeze_amount']);
            $activity['today_unfreeze_amount'] = (float)$query->where('change_type', '流水解冻')->whereBetween('created_at', [$today, $end])->sum('amount');//今日流水解冻金额
            $activity['today_unfreeze_amount'] = abs($activity['today_unfreeze_amount']);

            //新增会员统计
            $query = User::where('from','<>',2);
            $totalUser = (float)$query->whereBetween('created_at', [$start, $end])->count('id');
            $todayUser = (float)$query->whereBetween('created_at', [$today, $end])->count('id');//今日新增会员

            //新增会员统计
            $query = User::where('from',2);
            $totalVirtualUser = (float)$query->whereBetween('created_at', [$start, $end])->count('id');
            $todayVirtualUser = (float)$query->whereBetween('created_at', [$today, $end])->count('id');//今日新增会员
            
            //第三方提现统计（RMB）
            $query = PaymentOrder::query();
            $query->whereBetween('created_at', [$start, $end]);
            $query1 = clone $query;
            $totalAmount = (float)$query1->where('type', 2)->sum('amount');
            $query1 = clone $query;
            $totalPending = (float)$query1->where('type', 2)->where('status', 0)->sum('amount');//待处理
            $query1 = clone $query;
            $totalProcessing = (float)$query1->where('type', 2)->where('status', 1)->sum('amount');//处理中
            $query1 = clone $query;
            $totalSuccess = (float)$query1->where('type', 2)->where('status', 2)->sum('amount');//成功
            $query1 = clone $query;
            $totalFail = (float)$query1->where('type', 2)->where('status', 3)->sum('amount'); //  失败

            
            //第三方提现统计（RMB）今日
            $query = PaymentOrder::query();
            $query->whereBetween('created_at', [$today, $end]);
            $query1 = clone $query;
            $todayAmount = (float)$query1->where('type', 2)->sum('amount');
            $query1 = clone $query;
            $todayPending = (float)$query1->where('type', 2)->where('status', 0)->sum('amount');//待处理
            $query1 = clone $query;
            $todayProcessing = (float)$query1->where('type', 2)->where('status', 1)->sum('amount');//处理中
            $query1 = clone $query;
            $todaySuccess = (float)$query1->where('type', 2)->where('status', 2)->sum('amount');//成功
            $query1 = clone $query;
            $todayFail = (float)$query1->where('type', 2)->where('status', 3)->sum('amount'); //  失败


            //第三方充值统计（RMB）
            $query1 = clone $query;
            $rechargeRmbTotalAmount = (float)$query1->where('type', 1)->sum('amount');//总金额
            $query1 = clone $query;
            $rechargeRmbTotalPending = (float)$query1->where('type', 1)->where('status', 0)->sum('amount');//待处理
            $query1 = clone $query;
            $rechargeRmbTotalProcessing = (float)$query1->where('type', 1)->where('status', 1)->sum('amount');//处理中
            $query1 = clone $query;
            $rechargeRmbTotalSuccess = (float)$query1->where('type', 1)->where('status', 2)->sum('amount');//成功
            $query1 = clone $query;
            $rechargeRmbTotalFail = (float)$query1->where('type', 1)->where('status', 3)->sum('amount');//失败

            
            //第三方充值统计（RMB）今日
            $query1 = clone $query;
            $rechargeRmbTodayAmount = (float)$query1->where('type', 1)->sum('amount');//总金额
            $query1 = clone $query;
            $rechargeRmbTodayPending = (float)$query1->where('type', 1)->where('status', 0)->sum('amount');//待处理
            $query1 = clone $query;
            $rechargeRmbTodayProcessing = (float)$query1->where('type', 1)->where('status', 1)->sum('amount');//处理中
            $query1 = clone $query;
            $rechargeRmbTodaySuccess = (float)$query1->where('type', 1)->where('status', 2)->sum('amount');//成功
            $query1 = clone $query;
            $rechargeRmbTodayFail = (float)$query1->where('type', 1)->where('status', 3)->sum('amount');//失败

            //USDT提现统计
            $query = Withdraw::query();
            $query->whereBetween('created_at', [$start, $end]);
            $query1 = clone $query;
            $usdTotalAmount = (float)$query1->sum('amount');//总金额
            $query1 = clone $query;
            $usdTotalSuccess = (float)$query1->where('status', 1)->sum('amount');//成功
            $query1 = clone $query;
            $usdTotalFail = (float)$query1->where('status', 2)->sum('amount');//失败
            
            //USDT提现统计（今日）
            $query = Withdraw::query();
            $query->whereBetween('created_at', [$today, $end]);
            $query1 = clone $query;
            $usdTodayAmount = (float)$query1->sum('amount');//总金额
            $query1 = clone $query;
            $usdTodaySuccess = (float)$query1->where('status', 1)->sum('amount');//成功
            $query1 = clone $query;
            $usdTodayFail = (float)$query1->where('status', 2)->sum('amount');//失败
            
            //USDT提现统计（今日）
            $query = Withdraw::query();
            $query->whereBetween('created_at', [$today, $end]);
            $query1 = clone $query;
            $usdTodayAmount = (float)$query1->sum('amount');//总金额
            $query1 = clone $query;
            $usdTodaySuccess = (float)$query1->where('status', 1)->sum('amount');//成功
            $query1 = clone $query;
            $usdTodayFail = (float)$query1->where('status', 2)->sum('amount');//失败


            //USDT充值统计
            $query = Recharge::query();
            $query->whereBetween('created_at', [$start, $end]);
            $query1 = clone $query;
            $rechargeTotalAmount = (float)$query1->sum('amount');//总充值
            $query1 = clone $query;
            $rechargeTotalSuccess = (float)$query1->where('status', 1)->sum('amount');//成功
            $query1 = clone $query;
            $rechargeTotalFail = (float)$query1->where('status', 2)->sum('amount');//失败

            //USDT充值（今日）
            $query = Recharge::query();
            $query->whereBetween('created_at', [$today, $end]);
            $query1 = clone $query;
            $rechargeTodayAmount = (float)$query1->sum('amount');//总充值
            $query1 = clone $query;
            $rechargeTodaySuccess = (float)$query1->where('status', 1)->sum('amount');//成功
            $query1 = clone $query;
            $rechargeTodayFail = (float)$query1->where('status', 2)->sum('amount');//失败

            $result = [
                'start' => $start,
                'end' => $end,
                'bet' => $bet,//投注相关的
                'virtual_bet' => $virtual_bet,//虚拟投注相关的
                'activity' => $activity,//活动相关的
                'yuebao' => $yuebao,//余额宝相关的
                'recharge_usd' => [
                    'total_amount' => $rechargeTotalAmount,
                    'total_success' => $rechargeTotalSuccess,
                    'total_fail' => $rechargeTotalFail,

                    'today_amount' => $rechargeTodayAmount,
                    'today_success' => $rechargeTodaySuccess,
                    'today_fail' => $rechargeTodayFail,
                ],
                'recharge_rmb' => [
                    'total_amount' => $rechargeRmbTotalAmount,
                    'total_success' => $rechargeRmbTotalSuccess,
                    'total_fail' => $rechargeRmbTotalFail,
                    'total_processing' => $rechargeRmbTotalProcessing,
                    'total_pending' => $rechargeRmbTotalPending,

                    'today_amount' => $rechargeRmbTodayAmount,
                    'today_success' => $rechargeRmbTodaySuccess,
                    'today_fail' => $rechargeRmbTodayFail,
                    'today_processing' => $rechargeRmbTodayProcessing,
                    'today_pending' => $rechargeRmbTodayPending,
                ],

                'withdraw_usdt' => [
                    'total_fail' => $usdTotalFail,
                    'total_success' => $usdTotalSuccess,
                    'total_amount' => $usdTotalAmount,

                    'today_amount' => $usdTodayAmount,
                    'today_success' => $usdTodaySuccess,
                    'today_fail' => $usdTodayFail,
                ],
                'withdraw_rmb' => [
                    'total_fail' => $totalFail,
                    'total_success' => $totalSuccess,
                    'total_amount' => $totalAmount,
                    'total_processing' => $totalProcessing,
                    'total_pending' => $totalPending,

                    'today_amount' => $todayAmount,
                    'today_success' => $todaySuccess,
                    'today_fail' => $todayFail,
                    'today_processing' => $todayProcessing,
                    'today_pending' => $todayPending,
                ],
                'total_user' => $totalUser,
                'today_user' => $todayUser,
                'total_virtual_user' => $totalVirtualUser,
                'today_virtual_user' => $todayVirtualUser,

            ];
        } catch (ValidationException $e) {
            return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
        } catch (Exception $e) {
            return $this->error($e->getCode(), $e->getmessage());
        }
        return $this->success($result);
    }
}