<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Betting;
use App\Models\Bonus;
use App\Models\Commission;
use App\Models\Interest;
use App\Models\User;
use App\Models\RechargeSummery;
use App\Models\UpiTransaction;
use App\Models\Website;
use App\Models\Withdrawal;
use Carbon\Carbon;
use Illuminate\Support\Facades\Http;

class AdminDashboardController extends Controller
{
    function generateRandomString($length = 10)
    {
        $characters = '0123456789';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[random_int(0, $charactersLength - 1)];
        }
        return $randomString;
    }

    public function Dashboard()
    {

        // return 'hi';

        $all_users = User::where('role', '!=', 'admin')->get();
        $seven_days_date = Carbon::today()->subDays(7);
        $users_count = $all_users->count();
        $seven_days_users = $all_users->where('created_at', '>=', $seven_days_date)->count();
        $today_users = $all_users->where('created_at', '>=', date('Y-m-d'))->count();

        $recharge_requests = RechargeSummery::all();
        $total_recharge = $recharge_requests->where('status', '=', 1)->sum("amount");
        $pending_recharge_requests_count = $recharge_requests->where('status', '=', 0)->count();
        $approved_recharge_requests_count = $recharge_requests->where('status', '=', 1)->count();

        $withdrawal_requests = Withdrawal::all();
        $pending_withdrawal_requests_count = $withdrawal_requests->where('status', '=', 0)->count();
        $approved_withdrawal_requests_count = $withdrawal_requests->where('status', '=', 1)->count();
        $total_withdraw = $withdrawal_requests->where('status', '=', 1)->sum("payout");
        $amount_clearing = $withdrawal_requests->where('status', '=', 0)->sum("amount");

        $wallet_sum = round($all_users->sum('amount') + $amount_clearing, 2);



        $advance_color = env('ADVANCE_COLOR');
        if (!$advance_color) {
            $total_bonus = '...';
            $today_bonus = '...';
            $total_commission = '...';
            $today_commission = '...';
            $total_interest = '...';
            $today_interest = '...';
            $winning_amount = '...';
            $today_winning_amount = '...';
            $betting_amount = '...';
        } else {
            $bonus = Bonus::all();
            $total_bonus = round($bonus->sum('bonus'), 2);
            $today_bonus = round($bonus->where('created_at', '>=', date('Y-m-d'))->sum('bonus'), 2);

            $commission = Commission::all();
            $total_commission = round($commission->sum('commission'), 2);
            $today_commission = round($commission->where('created_at', '>=', date('Y-m-d'))->sum('commission'), 2);

            $interest = Interest::all();
            $total_interest = round($interest->sum('interest'), 2);
            $today_interest = round($interest->where('created_at', '>=', date('Y-m-d'))->sum('interest'), 2);

            $betting = Betting::all();
            $winning_amount = round($betting->sum('winning_amount'), 2);
            $today_winning_amount = round($betting->where('created_at', '>=', date('Y-m-d'))->sum('winning_amount'), 2);
            $betting_amount = round($betting->where('created_at', '>=', date('Y-m-d'))->sum('amount'), 2);
        }



        return view('adminDashboard.dashboard', compact('users_count', 'total_recharge', 'pending_withdrawal_requests_count', 'approved_withdrawal_requests_count', 'total_withdraw', 'wallet_sum', 'total_bonus', 'today_bonus', 'total_commission', 'today_commission', 'total_interest', 'today_interest', 'winning_amount', 'today_winning_amount', 'betting_amount', 'seven_days_users', 'today_users', 'pending_recharge_requests_count', 'approved_recharge_requests_count'));
    }
}
