<?php

namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use App\Models\User;
use App\Models\Settings;
use App\Models\Plans;
use App\Models\Transaction;
use App\Models\Signal_trades;
use App\Models\Agent;
use App\Models\User_plans;
use App\Models\Admin;
use App\Models\Faq;
use App\Models\Images;
use App\Models\Testimony;
use App\Models\Content;
use App\Models\Asset;
use App\Models\Mt4Details;
use App\Models\Deposit;
use App\Models\Notification;
use App\Models\Wdmethod;
use App\Models\Withdrawal;
use App\Models\Cp_transaction;
use App\Models\Tp_Transaction;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Mail\NewNotification;
use App\Mail\UserUpload;
use App\Mail\KycUpload;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use App\Http\Traits\CPTrait;

class ViewsController extends Controller
{


    public function dashboard(Request $request){
        
        $settings=Settings::where('id','1')->first();
        
        //Check if the user is referred by someone after a successful registration
        $settings=Settings::where('id','1')->first();
        
        //check for users without ref link and update them with it
        $usf=User::all();
        foreach($usf as $usf){
            //if the ref_link column is empty
            if($usf->ref_link==''){
                User::where('id', $usf->id)
                ->update([
                    'ref_link' => $settings->site_address.'/ref/'.$usf->username,
                    'ref_bonus' => '0',
                    'bonus_released' => '0',
                ]);
            }
            //give reg bonus if new
            if($usf->created_at->diffInDays() < 2 && $usf->signup_bonus!="received"){
                User::where('id', $usf->id)
                ->update([
                    'bonus' =>$usf->bonus + $settings->signup_bonus,
                    'account_bal' => $usf->account_bal + $settings->signup_bonus,
                    'signup_bonus' => "received",
                ]);

                if ($settings->signup_bonus > 0 and !empty($settings->signup_bonus)) {
                    //create history
                    Tp_Transaction::create([
                        'user' => Auth::user()->id,
                        'plan' => "SignUp Bonus",
                        'amount'=>$settings->signup_bonus,
                        'type'=>"Bonus",
                    ]); 
                }
                
            }
        } 
          
        //get referral earnings
          
        $dref=Agent::where('agent',Auth::user()->id)->first();
        if($dref){
            $ref_earnings = "0.00";
        }else{
           $ref_earnings = "$dref->earnings";
        }

        //sum total deposited
        $total_deposited = DB::table('transactions')->select(DB::raw("SUM(amount) as count"))->where('user', Auth::user()->id)->where('type','Deposit')->where('status','Processed')->get();
        
        if($settings->payment_mode=='Bank transfer'){
          $condition=empty(Auth::user()->account_no) or empty(Auth::user()->account_name) or empty(Auth::user()->bank_name) or empty(Auth::user()->phone);
        }elseif($settings->payment_mode=='BTC'){
          $condition=empty(Auth::user()->btc_address) or empty(Auth::user()->phone);
        }elseif($settings->payment_mode=='ETH'){
          $condition=empty(Auth::user()->eth_address) or empty(Auth::user()->phone);
        }else{
          $condition=empty(Auth::user()->id);
        }

        //Get bonus from users table
        $total_bonus = User::where('id', Auth::user()->id)->first();

        //count the number of plans users have purchased
        $user_plan = User_plans::where('user', Auth::user()->id)->get();

        $user_plan_active = User_plans::where([
                    ['user', '=', Auth::user()->id],
                    ['active', '=', 'yes']
                ])->get();
        
        //log user out if not approved
        if(Auth::user()->status != "active"){
            $request->session()->flush();
            $request->session()->put('reged','yes');
            return redirect()->route('dashboard');
        }//Also log user out if web dashboard is not enabled and user is not admin
        
        return view('user.dashboard' ,[
            'title'=>'User panel',
            'ref_earnings' => $ref_earnings,
            'deposited' => $total_deposited,
            'total_bonus' => $total_bonus,
            'user_plan' => $user_plan,
            'user_plan_active'=> $user_plan_active,
            'deposits' => Transaction::where('user', Auth::user()->id)->orderBy('id', 'desc')
          ->get(),
            'upplan' => Plans::where('id', Auth::user()->promo_plan)->first(),
            'uplan' => Plans::where('id', Auth::user()->plan)->first(),
            
        ]);
    }  

    //Profile route
    public function profile(){
         $total_deposited = DB::table('transactions')->select(DB::raw("SUM(amount) as count"))->where('user', Auth::user()->id)->where('type','Deposit')->where('status','Processed')->get();
        $settings = Settings::where('id', 1)->first();
        $userinfo = User::where('id',Auth::user()->id)->first();
        return view('user.profile')->with(array(
            'userinfo' => $userinfo,
            'title' => 'Profile',
            'deposited' => $total_deposited,
            
        ));
    }

    //return add withdrawal account form view
    public function accountdetails(){
       
        return view('user.updateacct')->with(array(
          'title'=>'Update account details',
          
        ));
    }

    //Notification page view
    public function notification(){
        return view('user.notification', [
            'Notif' => Notification::where('user_id',Auth::user()->id)->orderBy('id', 'desc')->paginate(12),
            'title' => 'Notification',
            
        ]);
    }

    //support route
    public function support()
    {
    	return view('user.support')
        ->with(array(
            'title'=>'Support',
            
        ));
    }
    
   
    
    // deposit_hist route
    public function  deposit_hist(){
         $total_deposited = DB::table('transactions')->select(DB::raw("SUM(amount) as count"))->where('user', Auth::user()->id)->where('type','Deposit')->where('status','Processed')->get();
        return view('user.deposit_hist')
        ->with(array(
             'deposits' => Transaction::where('user', Auth::user()->id)->orderBy('id', 'desc')
          ->get(),
         
          'title' => 'Transaction History',
           'deposited' => $total_deposited,
          
        ));
    }
    

    //Trading history route
    public function tradinghistory(){
        $total_deposited = DB::table('transactions')->select(DB::raw("SUM(amount) as count"))->where('user', Auth::user()->id)->where('type','Deposit')->where('status','Processed')->get();
        return view('user.thistory')
        ->with(array(
          't_history' => Tp_Transaction::where('user',Auth::user()->id)
          ->where('type','ROI')
          ->orderBy('id', 'desc')
          ->get(),
          'title' => 'Trading History',
          'deposited' => $total_deposited,
        ));
    }
    
    //Trading history route
    public function withdraws_hist(){
         $total_deposited = DB::table('transactions')->select(DB::raw("SUM(amount) as count"))->where('user', Auth::user()->id)->where('type','Deposit')->where('status','Processed')->get();
        return view('user.withdraws_hist')
        ->with(array(
          
            'withdrawals' => Withdrawal::where('user', Auth::user()->id)->orderBy('id', 'desc')
          ->get(),
          'title' => 'Withdrawal History',
          'deposited' => $total_deposited,
          
        ));
    }

    //Account transactions history route
    public function accounthistory(){
         $total_deposited = DB::table('transactions')->select(DB::raw("SUM(amount) as count"))->where('user', Auth::user()->id)->where('type','Deposit')->where('status','Processed')->get();
        return view('user.transactions')
        ->with(array(
          't_history' => Tp_Transaction::where('user',Auth::user()->id)
          ->where('type','<>','ROI')
          ->orderBy('id', 'desc')
          ->get(),

          'withdrawals' => Withdrawal::where('user', Auth::user()->id)->orderBy('id', 'desc')
          ->get(),
          'deposits' => Deposit::where('user', Auth::user()->id)->orderBy('id', 'desc')
          ->get(),
          'title' => 'Account Transactions History',
           'deposited' => $total_deposited,
        ));
    }

    //Return deposit route
    public function deposits()
    {
          $total_deposited = DB::table('transactions')->select(DB::raw("SUM(amount) as count"))->where('user', Auth::user()->id)->where('type','Deposit')->where('status','Processed')->get();
        $paymethod = Wdmethod::where('type', 'deposit')->orWhere('type', 'both')->where('status', 'enabled')->orderByDesc('id')->get();
    	return view('user.deposits')
        ->with(array(
            'deposited' => $total_deposited,
            'title'=>'Fund your account',
            'dmethods' => $paymethod,
            'deposits' => Deposit::where(['user'=>Auth::user()->id])
            ->orderBy('id', 'desc')
            ->get(),
            
        ));
    } 



///Signal route

 public function signal()
    {
        
          $total_deposited = DB::table('transactions')->select(DB::raw("SUM(amount) as count"))->where('user', Auth::user()->id)->where('type','Deposit')->where('status','Processed')->get();
        $paymethod = Wdmethod::where('type', 'deposit')->orWhere('type', 'both')->where('status', 'enabled')->orderByDesc('id')->get();
        $signal = Signal_trades::where('type','main')->get();
    	return view('user.signal')
        ->with(array(
             'deposited' => $total_deposited,
            'title'=>'Purchase Signal',
            'dmethods' => $paymethod,
            'signals' => $signal,
            'deposits' => Deposit::where(['user'=>Auth::user()->id])
            ->orderBy('id', 'desc')
            ->get(),
            
        ));
    } 
 
    
   
     //Return withdrawals route
     public function withdrawals()
     {
          $total_deposited = DB::table('transactions')->select(DB::raw("SUM(amount) as count"))->where('user', Auth::user()->id)->where('type','Deposit')->where('status','Processed')->get();
          $settings=Settings::where('id','1')->first();
          if(Auth::user()->action =='Yes'){
              $currency  = $settings->currency;
              $amount = Auth::user()->amount;
              return redirect()->back()->with('message','YOU ARE REQUIRED TO MAKE AN ACCOUNT UPGRADE OF '. $currency.$amount. ' IMMEDIATELY!!!');
            
        }
         
        $withdrawals = Wdmethod::where('type', 'withdrawal')->orWhere('type', 'both')->where('status', 'enabled')->orderByDesc('id')->get();

        return view('user.withdrawals')
         ->with(array(
            'title'=>'Withdraw Your funds',
            'wmethods' => $withdrawals,
             'deposited' => $total_deposited,
         ));
     }
     
    //Subscription Trading 
    public function subtrade(Request $request)
    {
        
         $total_deposited = DB::table('transactions')->select(DB::raw("SUM(amount) as count"))->where('user', Auth::user()->id)->where('type','Deposit')->where('status','Processed')->get();
        $settings = Settings::where('id', 1)->first();

        if($settings->subscription_service != 'on'){
            return redirect()->back();
        }
        return view('user.subtrade')
        ->with(array(
            'deposited' => $total_deposited,
            'title'=>'Subscription Trade',
            'subscriptions' => Mt4Details::where('client_id', auth::user()->id)->orderBy('id', 'desc')->get(),
            
        ));
    } 
     
    
    //Main Plans route
    public function mplans()
    {
         $total_deposited = DB::table('transactions')->select(DB::raw("SUM(amount) as count"))->where('user', Auth::user()->id)->where('type','Deposit')->where('status','Processed')->get();
        $settings = Settings::where('id', 1)->first();
        $paymethod = Wdmethod::where('type', 'deposit')->orWhere('type', 'both')->where('status', 'enabled')->orderByDesc('id')->get();
    	return view('user.mplans')
        ->with(array(
            'title'=>'Account Upgrade',
             'dmethods' => $paymethod,
             'deposited' => $total_deposited,
            'plans'=> Plans::where('type', 'main')->get(),
            'settings' => Settings::where('id','1')->first(),
        ));
    }
    
     public function plan()
    {
         $total_deposited = DB::table('transactions')->select(DB::raw("SUM(amount) as count"))->where('user', Auth::user()->id)->where('type','Deposit')->where('status','Processed')->get();
        $settings = Settings::where('id', 1)->first();
        $paymethod = Wdmethod::where('type', 'deposit')->orWhere('type', 'both')->where('status', 'enabled')->orderByDesc('id')->get();
    	return view('user.plans')
        ->with(array(
            'title'=>'Account Upgrade',
             'dmethods' => $paymethod,
             'deposited' => $total_deposited,
            'plans'=> Plans::where('type', 'main')->get(),
            'settings' => Settings::where('id','1')->first(),
        ));
    }
    
    //My Plans route
    public function myplans()
    {
         $total_deposited = DB::table('transactions')->select(DB::raw("SUM(amount) as count"))->where('user', Auth::user()->id)->where('type','Deposit')->where('status','Processed')->get();
        $settings = Settings::where('id', 1)->first();
        $plans=User_plans::where('user', Auth::user()->id)->get();
        if(count($plans)<1){
            return redirect()->back()->with('message','You do not have a package at the moment');
        }
    	return view('user.myplans')
        ->with(array(
            'title'=>'Your packages',
            'deposited' => $total_deposited,
            'plans'=> User_plans::where('user', Auth::user()->id)->get(),
            'cplan'=> User_plans::where('id', Auth::user()->user_plan)->first(),
            'settings' => Settings::where('id','1')->first(),
        ));
    }

    function twofa() {
         $total_deposited = DB::table('transactions')->select(DB::raw("SUM(amount) as count"))->where('user', Auth::user()->id)->where('type','Deposit')->where('status','Processed')->get();
        $settings = Settings::where('id', 1)->first();
        return view('profile.show', [
            'title' => 'Advance Security Settings',
             'deposited' => $total_deposited,
        ]);
    }

    // Referral Page
    public function referuser(){
        return view('user.referuser', [
            'title'=>'Refer user',
        ]);
    }

    function verifyaccount() {
         $total_deposited = DB::table('transactions')->select(DB::raw("SUM(amount) as count"))->where('user', Auth::user()->id)->where('type','Deposit')->where('status','Processed')->get();
        return view('user.verify', [
            
          'title' => 'Verify your Account',
          'deposited' => $total_deposited,
        ]);
    }
     
























}
