<?php

namespace App\Http\Controllers;

use App\Lib\Mlm;
use App\Models\Transaction;
use App\Models\UserExtra;
use App\Models\Investment;
use App\Models\Plan;
use App\Models\User;
use App\Models\BvLog;
use Carbon\Carbon;

class CronController extends Controller
{
    public function cron()
    {
        $mlm = new Mlm();
        $general = gs();
        $general->last_cron = now()->toDateTimeString();
        $general->save();

      //  $check = $mlm->checkTime();
      //  if (!$check) {
      //      return 0;
      //  }

        $general->last_paid = now()->toDateString();
        $general->save();

      $check = $mlm->checkTime();
        if ($check) {
         $eligibleUsers = UserExtra::where('bv_left', '>=', $general->total_bv)->where('bv_right', '>=', $general->total_bv)->cursor() ;
         foreach ($eligibleUsers as $uex) {
          $user = $uex->user;
            $bvleft=$uex->bv_left;
            $bvright=$uex->bv_right;
          if( $bvright >= $bvleft)
           {
               $bonus= $bvleft*0.08;
               $user->balance += $bonus;
               $user->total_binary_com += $bonus;
               $user->save();
               //create transaction
               $transaction = new Transaction();
               $transaction->user_id = $user->id;
               $transaction->amount = $bonus;
               $transaction->post_balance = $user->balance;
               $transaction->charge = 0;
               $transaction->trx_type = '+';
               $transaction->remark = 'binary_commission';
               $transaction->details = 'Paid ' . $bonus . ' ' . $general->cur_text .' For ' . $bvleft . ' Business.' ;
               $transaction->trx =  getTrx();
               $transaction->save();
      
                    $uex->bv_left -= $bvleft;
                    $uex->bv_right -= $bvleft;
                    $uex->save();
                    
                $bv_log = new BvLog();
                $bv_log->user_id = $user->id;
                $bv_log->position = 1;
                $bv_log->amount = $bvleft;
                $bv_log->inv_amount = $bvleft;
                $bv_log->trx_type = '-';
                $bv_log->details = 'Flush ' . showAmount($bvleft) . ' BV after Paid ' . showAmount($bonus) . ' ' . __($general->cur_text) . ' For 8% of ' . showAmount($bvleft) . ' BV.';
                $bv_log->save();
      
          }else if( $bvleft >= $bvright){
                $bonus= $bvright*0.08 ;
                $user->balance += $bonus;
                $user->total_binary_com += $bonus;
                 $user->save();
      
                //create transaction
                $transaction = new Transaction();
                $transaction->user_id = $user->id;
                $transaction->amount = $bonus;
                $transaction->post_balance = $user->balance;
                $transaction->charge = 0;
                $transaction->trx_type = '+';
                $transaction->remark = 'binary_commission';
                $transaction->details = 'Paid ' . $bonus . ' ' . $general->cur_text . ' For ' . $bvright . ' Business.' ;
                $transaction->trx =  getTrx();
                $transaction->save();
      
                    $uex->bv_left -= $bvright;
                    $uex->bv_right -= $bvright;
                    $uex->save();
                    
                    $bv_log = new BvLog();
                    $bv_log->user_id = $user->id;
                    $bv_log->position = 2;
                    $bv_log->amount = $bvright;
                    $bv_log->inv_amount = $bvright;
                    $bv_log->trx_type = '-';
                    $bv_log->details = 'Flush ' . showAmount($bvright) . ' BV after Paid ' . showAmount($bonus) . ' ' . __($general->cur_text) . ' For 8% of ' . showAmount($bvright) . ' BV.';
                    $bv_log->save();
            }
          }
        }else{
      }
        
        $now = Carbon::now();
        $cronplan=  Plan::where('id','1')->first();
         $cron_status=$cronplan->cron;
         $cron_time=$cronplan->next_time;
         
          if($cron_status == 1 &&  $cron_time <= $now){
         $investments = Investment::whereHas('user')->whereStatus(1)->with(['user:id,firstname,lastname,username,email,country_code,mobile,balance,total_ROI_com'])->get();
         foreach ($investments as $data){
                $plan_id=$data->plan_id;
                
                $user_plan=  Plan::where('id',$plan_id)->first();
                $plan_profit=$user_plan->profit;
                
                $invested_amount=$data->amount;
                $amontadd=$invested_amount*$plan_profit/100;
                
                $next_time = Carbon::parse($now)->addHours($data->point_in_time);
                $invest= $data;
                $invest->recurring_time += 1;
                $invest->afterward = $next_time; // next Profit will get
                $invest->formerly = $now; // Last Time Get Profit
                  $invest->save();
                
                $user = $data->user;
                $new_balance = getAmount($user->total_ROI_com + $amontadd);
                $user->total_ROI_com = $new_balance;
                $user->balance += $amontadd;
                $user->save();
                
                
            $user = $data->user;
             $transaction = new Transaction();
            $transaction->user_id = $user->id;
            $transaction->amount = $amontadd;
            $transaction->post_balance = $user->balance;
            $transaction->charge = 0;
            $transaction->trx_type = '+';
            $transaction->remark = 'ROI Comission';
            $transaction->details = 'Amount of ' . $amontadd . ' ' . $general->cur_text . ' For ROI Comission ADDed in Your Account' ;
            $transaction->trx =  getTrx();
            $transaction->save();
            
          $alltransaction= Transaction::where('trx_type','+')->where('user_id',$user->id)->where('remark','!=','deposit')->sum('amount');
           
           $check_isnetwork=User::where('ref_by', $data->user)->first();
           $userx=User::where('id', $user->id)->first();
           if($check_isnetwork){
              $amount= $userx->total_invest*5;
              if($alltransaction >= $amount)
              {
                   $invest= $data;
                   $invest->status = 0;
                   $invest->save();
                    $userx->plan_id="";
                    $userx->save();
              }
              
           }else{
                  $amount= $userx->total_invest*2;
                  if($alltransaction >= $amount)
                  {
                   $invest= $data;
                   $invest->status = 0;
                   $invest->save();
                    $userx->plan_id="";
                    $userx->save();
                   }
                }
           
         }
          $cronplan->cron= '0';
          $cronplan->save();
         
         }
    }
}
