<?php

namespace App\Console\Commands\MerchantsScripts;

use App\User;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Yajny\Member\Models\CurrencyRate;
use Yajny\Member\Models\Member;
use Yajny\Member\Models\UserBonus;
use Yajny\Member\Models\UserSales;

class MerchantsReferAndEarnScript extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'merchant_referrer:run';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Give referrer merchants bonus for refered new merchants';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $currencyRate = CurrencyRate::where('start_date', '<', Carbon::now())
            ->where('currency_code', 'EGP')
            ->orderBy('start_date', 'desc')->first()->conversion_rate;
        $merchants=Member::where('is_merchant',true)
            ->where("active",1)
            ->where("referrer_code",'<>','')
            ->whereNotNull("referrer_code")
            ->where("parent_bonus",false)->get();
        foreach ($merchants as $merchant){
            $firstOrder=UserSales::where("user_id",$merchant->user_id)->where("status","confirmed")->first();

            // checking if the user have an order or not

            if ($firstOrder){
                // checking the date
                $endDate=$merchant->created_at->addMonths(3);

                if($endDate->addMonths(3)<Carbon::now()){
                    $merchant->parent_bonus=1;

                    $merchant->save();
                    $cashback=UserSales::where('user_id',$merchant->user_id)
                        ->where([['status', 'confirmed'], ['is_reset', false]])
                        ->whereBetween('created_at',[$merchant->created_at,$endDate])
                        ->select(DB::raw("SUM(cashback) as cashback , SUM(cashback_egp) as cashback_egp"))
                        ->first();
                    if ($cashback->cashback_egp>0){
                        $parent=Member::where("referral_code",$merchant->referrer_code)->first();
                        $bonus = New UserBonus();
                        $bonus->user_id=$parent->user_id;
                        $bonus->bonus_code="referrer_bonus";
                        $bonus->awarded_on=Carbon::now()->format('Y-m-d');
                        $bonus->expires_on=Carbon::now()->addDays(90);
                        $bonus->status='confirmed';
                        $bonus->qualifying_amount=25;
                        $bonus->currency='EGP';
                        $bonus->currency_rate=$currencyRate;
                        $bonus->is_reset=false;
                        $bonus->amount=$cashback->cashback*0.1;
                        $bonus->local_amount=$cashback->cashback_egp*0.10;
                        $bonus->save();

                    }
                }


            }

        }

    }




}
