<?php /** @noinspection PhpUndefinedMethodInspection */

namespace App\Providers;

use App\category;
use App\Client;
use App\Resolution;
use App\Voters;
use App\Votes;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(100);

        $allResolutions=Resolution::query()->get();

        foreach ($allResolutions AS $resolution){
            $id= $resolution->title;


        }



        view()->composer('*', function ($view) {

            $res1 = DB::select(
                "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE approval_of_final_dividend=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE approval_of_final_dividend=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE approval_of_final_dividend=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE approval_of_final_dividend=0) AS ag_num,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE approval_of_final_dividend=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE approval_of_final_dividend=3) AS abstain_num
              );
                    ");

            $res2 = DB::select(
                "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE ebenezer_twum_asante=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE ebenezer_twum_asante=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE ebenezer_twum_asante=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE ebenezer_twum_asante=0) AS ag_num,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE ebenezer_twum_asante=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE ebenezer_twum_asante=3) AS abstain_num
              );");


            $res3 = DB::select(
                "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE modupe_agbolade_kadri=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE modupe_agbolade_kadri=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE modupe_agbolade_kadri=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE modupe_agbolade_kadri=0) AS ag_num,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE modupe_agbolade_kadri=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE modupe_agbolade_kadri=3) AS abstain_num
              );");

            $res4 = DB::select(
                "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE fatima_daniels=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE fatima_daniels=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE fatima_daniels=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE fatima_daniels=0) AS ag_num,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE fatima_daniels=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE fatima_daniels=3) AS abstain_num
              );");

            $res5 = DB::select(
                "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE approval_of_directors_remuneration=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE approval_of_directors_remuneration=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE approval_of_directors_remuneration=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE approval_of_directors_remuneration=0) AS ag_num,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE approval_of_directors_remuneration=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE approval_of_directors_remuneration=3) AS abstain_num
              );");


            $res6 = DB::select(
                "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE authorization_of_directors=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE authorization_of_directors=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE authorization_of_directors=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE authorization_of_directors=0) AS ag_num,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE authorization_of_directors=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE authorization_of_directors=3) AS abstain_num
              );");


            $res7 = DB::select(
                "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE resolution7=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE resolution7=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE resolution7=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE resolution7=0) AS ag_num,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE resolution7=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE resolution7=3) AS abstain_num
              );");


            $res8 = DB::select(
                "SELECT * FROM (
                    (SELECT SUM(percentage) AS `for` FROM votes WHERE resolution8=1) AS yes,
               (SELECT count(id) AS for_total FROM votes WHERE resolution8=1) AS for_num,
               (SELECT SUM(percentage) AS against FROM votes WHERE resolution8=0) AS no,
               (SELECT count(id) AS against_total FROM votes WHERE resolution8=0) AS ag_num,
               (SELECT SUM(percentage) AS abstain FROM votes WHERE resolution8=3) AS abstain,
               (SELECT count(id) AS abstain_total FROM votes WHERE resolution8=3) AS abstain_num
              );");


            $response = [];
            $output = array();

            $response2 = [];
            $output2 = array();

            $response3 = [];
            $output3 = array();

            $response4 = [];
            $output4 = array();


            $response5 = [];
            $output5 = array();


            $response6 = [];
            $output6 = array();

            $response7 = [];
            $output7 = array();

            $response8 = [];
            $output8 = array();


            foreach ($res1 as $value){
                $response['for_total']     =  $value->for_total;
                $response['against_total'] =  $value->against_total;
                $response['abstain_total'] =  $value->abstain_total;
                $response['for'] =  !empty($value->for) ? $value->for : 0;
                $response['against'] =  !empty($value->against) ? $value->against : 0;
                $response['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output,$response);
            }

            foreach ($res2 as $value){
                $response2['for_total']     =  $value->for_total;
                $response2['against_total'] =  $value->against_total;
                $response2['abstain_total'] =  $value->abstain_total;
                $response2['for'] = !empty($value->for) ? $value->for : 0;
                $response2['against'] = !empty($value->against) ? $value->against : 0;
                $response2['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output2,$response2);
            }

            foreach ($res3 as $value){
                $response3['for_total']     =  $value->for_total;
                $response3['against_total'] =  $value->against_total;
                $response3['abstain_total'] =  $value->abstain_total;
                $response3['for'] = !empty($value->for) ? $value->for : 0;
                $response3['against'] =  !empty($value->against) ? $value->against : 0;
                $response3['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output3,$response3);
            }

            foreach ($res4 as $value){
                $response4['for_total']     =  $value->for_total;
                $response4['against_total'] =  $value->against_total;
                $response4['abstain_total'] =  $value->abstain_total;
                $response4['for'] = !empty($value->for) ? $value->for : 0;
                $response4['against'] =  !empty($value->against) ? $value->against : 0;
                $response4['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output4,$response4);
            }

            foreach ($res5 as $value){
                $response5['for_total']     =  $value->for_total;
                $response5['against_total'] =  $value->against_total;
                $response5['abstain_total'] =  $value->abstain_total;
                $response5['for'] = !empty($value->for) ? $value->for : 0;
                $response5['against'] =  !empty($value->against) ? $value->against : 0;
                $response5['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output5,$response5);
            }

            foreach ($res6 as $value)
            {

                $response6['for_total']     =  $value->for_total;
                $response6['against_total'] =  $value->against_total;
                $response6['abstain_total'] =  $value->abstain_total;
                $response6['for'] = !empty($value->for) ? $value->for : 0;
                $response6['against'] =  !empty($value->against) ? $value->against : 0;
                $response6['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output6,$response6);
            }

            foreach ($res7 as $value)
            {

                $response7['for_total']     =  $value->for_total;
                $response7['against_total'] =  $value->against_total;
                $response7['abstain_total'] =  $value->abstain_total;
                $response7['for'] = !empty($value->for) ? $value->for : 0;
                $response7['against'] =  !empty($value->against) ? $value->against : 0;
                $response7['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output7,$response7);
            }

            foreach ($res8 as $value)
            {

                $response8['for_total']     =  $value->for_total;
                $response8['against_total'] =  $value->against_total;
                $response8['abstain_total'] =  $value->abstain_total;
                $response8['for'] = !empty($value->for) ? $value->for : 0;
                $response8['against'] =  !empty($value->against) ? $value->against : 0;
                $response8['abstain'] =  !empty($value->abstain) ? $value->abstain : 0;
                array_push($output8,$response8);
            }

            $allResolutions= Resolution::query()->get();
            if (!$allResolutions){
                $allResolutions=array();
            }
            $activeResolution= Resolution::query()->where('status',1)->first();

            if ($activeResolution){
                $activeResolution=$activeResolution;
            }else{
                $activeResolution=(object)["title"=>"No active resolution"];
            }

            $verified = Voters::query()->where('verified',true)->get()->count();
            $total = Votes::query()->get()->groupBy('msisdn')->count();

            $res1Voted = DB::select("SELECT * FROM votes WHERE approval_of_final_dividend IS NOT NULL AND approval_of_final_dividend <> ''");
            $res2Voted = DB::select("SELECT * FROM votes WHERE ebenezer_twum_asante IS NOT NULL");
            $res3Voted = DB::select("SELECT * FROM votes WHERE modupe_agbolade_kadri IS NOT NULL");
            $res4Voted = DB::select("SELECT * FROM votes WHERE fatima_daniels IS NOT NULL");
            $res5Voted = DB::select("SELECT * FROM votes WHERE approval_of_directors_remuneration IS NOT NULL");
            $res6Voted = DB::select("SELECT * FROM votes WHERE authorization_of_directors IS NOT NULL");
            $res7Voted = DB::select("SELECT * FROM votes WHERE resolution7 IS NOT NULL");
            $res8Voted = DB::select("SELECT * FROM votes WHERE resolution8 IS NOT NULL");


//            dd($output[0]);
            $view->with([

               'output1'=> $output[0],
               'output2'=> $output2[0],
               'output3'=> $output3[0],
               'output4'=> $output4[0],
               'output5'=> $output5[0],
               'output6'=> $output6[0],
               'output7'=> $output7[0],
               'output8'=> $output8[0],
                'verified' => $verified,
                'total_voted' => $total,
                'activeResolution'=>$activeResolution,
                'allResolutions'=>$allResolutions,
                'res1Voted'=>$res1Voted,
                'res2Voted'=>$res2Voted,
                'res3Voted'=>$res3Voted,
                'res4Voted'=>$res4Voted,
                'res5Voted'=>$res5Voted,
                'res6Voted'=>$res6Voted,
                'res7Voted'=>$res7Voted,
                'res8Voted'=>$res8Voted,
            ]);
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}