<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\ActivityLog;
use App\Models\Topup;
use IlluminateAgnostic\Arr\Support\Carbon;
use App\Models;
use DB;
class DashboardController extends Controller
{
    //
    public function index(){
        ## DB Tables
        /*
            - merchant_top_ups - from dealers to merchants
            - organisation_top_us - from Admin to dealers
            - transactions - from merchants to end users
        */

        $merchant = Models\Merchant::all()->count();
        $dealers = Models\OrgUser::all()->count();
        $activities = ActivityLog::with('admin')->orderBy('created_at', 'desc')->get()->take(10);
        $dealer_topups = Models\OrganisationTopUp::with('orgInfo','userInfo')->orderBy('created_at','desc')->get();
        $merchant_topups = Models\MerchantTopUp::with('merchantInfo','orgUserInfo')->orderBy('created_at','desc')->get();
    //    dd($merchant_topups);
        $current_year_transactions = Models\Transaction::whereYear('created_at', date('Y'))->get();
        $current_weeek_transactions = Models\Transaction::whereBetween('created_at', [now()->startOfWeek(), now()->endOfWeek()])->get();
        $last_weeek_transactions = Models\Transaction::whereBetween('created_at', [now()->subWeek()->startOfWeek(), now()->subWeek()->endOfWeek()])->get();
        $current_month_transactions = Models\Transaction::whereMonth('created_at', date('m'))->get();
        $current_day_transactions = Models\Transaction::whereDate('created_at', date('Y-m-d'))->get();
        #$top_merchants = Models\Transaction::select('merchant_id', 'amount')->with('merchant')->groupBy('amount', 'merchant_id')->orderByRaw('SUM(amount) DESC')->limit(4)->get();
        $top_merchants = DB::table('transactions')
                        ->select('transactions.merchant_id', 'merchants.fullname',   DB::raw('SUM(amount) as total_amount'))
                        ->join('merchants', 'transactions.merchant_id', '=', 'merchants.id')    
                        ->groupBy('fullname', 'merchant_id', 'amount')
                        ->orderByRaw('SUM(amount) DESC')
                        ->limit(4)
                        ->get();
        // dd($top_merchants);
        // $top_merchants_by_sales = Models\Merchant::with('transactions')->orderBy('transactions', 'desc')->get();
        // $top_dealers = Models\Organisation::with('transactions')->orderBy('transactions', 'desc')->get();
        $data = [
            'merchant_count' => $merchant,
            'dealer_count' => $dealers,
            'activities' => $activities,
            'dealer_topup_count' =>$dealer_topups->count(),
            'dealer_topup_sum' =>$dealer_topups->sum('amount'),
            'dealer_topups' => $dealer_topups->take(5),
            'merchant_topups' => $merchant_topups->take(10),
            'merchant_topup_count' => $merchant_topups->count(),
            'merchant_topup_sum' => $merchant_topups->sum('amount'),
            'current_weeek_transactions' => $current_weeek_transactions->sum('amount'),
            'current_month_transactions' => $current_month_transactions->sum('amount'),
            'last_weeek_transactions' => $last_weeek_transactions->sum('amount'),
            'current_day_transactions' => $current_day_transactions->sum('amount'),
            'top_merchants' => $top_merchants->take(5),
            // 'top_dealers' => $top_dealers->take(5),
            'transactions' => $this->transactions(),
            'current_year_transactions_sum' => $current_year_transactions->sum('amount'),
            'page_title' => 'Dashboard'
        ];
        // dd($data['top_merchants']);
        return view('dashboard.index', $data);
    }
    public function transactions(){
        $transactions = \DB::table('transactions')
                ->select(\DB::raw('SUM(amount) as total_sales, MONTH(created_at) as theMonth'))
                ->groupByRaw('month(created_at)')
                ->get();

        /*
        $transactions = Transaction::select('id', 'created_at')->get()
                ->groupBy(function($date) {
                    //return Carbon::parse($date->created_at)->format('Y'); // grouping by years
                    return Carbon::parse($date->created_at)->format('m'); // grouping by months
                });

        dd($transactions);

        $transactionmcount = [];
        $transactionArr = [];

        foreach ($transactions as $key => $value) {
            $transactionmcount[(int)$key] = count($value);
        }


        for($i = 1; $i <= 12; $i++){
            if(!empty($transactionmcount[$i])){
                $transactionArr[$i] = $transactionmcount[$i];
            }else{
                $transactionArr[$i] = 0;
            }
        }

        $transactions = '';
        foreach ($transactionArr as $tran) {
            $transactions = $transactions.','.$tran;
        }
        */
        return $transactions; //response()->json($transactions);

    }
    public function merchant(){
        $merchant_session = session('merchant');
        $merchant = Models\Merchant::find($merchant_session->id);
        $merchant_trans = Models\Transaction::where('merchant_id',$merchant->id)->orderBy('id', 'DESC')->get();
        $data = [
            'transaction_count' => $merchant_trans->count(),
            'transactions' => $merchant_trans->take(5),
            'current_balance' => $merchant['balance'],
            'transactions_graph' => $this->merchant_transactions($merchant),
        ];
        // dd($data);
        return view('dashboard.merchant',$data);
    }
    
    


}
