<?php

namespace App\Console\Commands;

use App\User;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Yajny\Member\Models\Member;
use Yajny\Member\Models\NewBonusType;
use Yajny\Member\Models\UserBonus;
use Yajny\Member\Models\UserNewBonus;
use Yajny\Member\Models\UserPayouts;
use Yajny\Member\Models\UserSales;
use Yajny\Member\Models\UsersBalance;

class rewardOldreferralcashbackBonus extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'rewards';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = "reward all referrer 10% of their child's cashbacks";

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $startTime = microtime(true);

        $cashbackBonus=NewBonusType::where('code',"RC")->first();
        // getting old confirmed sales to assign to referral 10% of cashback
        $CBTransactions=UserSales::where([["transaction_time",">=","2022-11-16 00:00:00"],["status",'confirmed'],["parent_bonus",'0']])->take(1000)->get();
        foreach ($CBTransactions as $CBTransaction){
            $CBowner=Member::find($CBTransaction->user_id);
           // $this->output->write('this is the id '.$CBowner->user_id,true);
            // checking if the user is not merchant
            if ($CBowner->is_merchant!=true){
                if ($CBowner->referrer_code){
                    // getting the parent user
                    $userParent=Member::where("referral_code",$CBowner->referrer_code)->first();
                    // assign 10% of cashback bonus for the parent as new bonus
                    if ($userParent)
                        DB::table("user_new_bonuses")->insert([
                        "user_id"=>$userParent->user_id,
                        "status"=>"confirmed",
                        "bonus_type_id"=>$cashbackBonus->id,
                        "awarded_points"=>0,
                        "expiration_date"=>Carbon::now()->addYear()->toDateTimeString(),
                        "awarded_on"=>Carbon::now()->toDateTimeString(),
                        "created_at"=>Carbon::now()->toDateTimeString(),
                        "updated_at"=>Carbon::now()->toDateTimeString(),
                        "amount"=>$CBTransaction->cashback * 0.1,
                        "currency"=>$CBTransaction->currency,
                        "local_amount"=>$CBTransaction->local_cashback * 0.1,
                        "is_referral"=>1,
                        "currency_rate"=>$CBTransaction->currency_rate,
                        "amount_egp"=>$CBTransaction->cashback_egp * 0.1,
                        "amount_sar"=>$CBTransaction->cashback_sar * 0.1,
                        "amount_aed"=>$CBTransaction->cashback_aed * 0.1,
                        "amount_kwd"=>$CBTransaction->cashback_kwd * 0.1,
                    ]);
                }
            }
            $CBTransaction->parent_bonus=true;
            $CBTransaction->save();
        }
        $endTime = microtime(true);
        $executionTime = $endTime - $startTime;
        Log::channel("separated_file")->info("The Script".class_basename($this)."   Took : $executionTime");

    }
}
