<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Yajny\Member\Models\CurrencyRate;
use Yajny\Member\Models\UserBonus;
use Yajny\Member\Models\UserNewBonus;
use Yajny\Member\Models\UserPayouts;
use Yajny\Member\Models\UserSales;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Get currency exchange rates for Cashback';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $startTime = microtime(true);
        $endTime = microtime(true);
        $executionTime = $endTime - $startTime;
        Log::channel("separated_file")->info("The Script".class_basename($this)."   Took : $executionTime");
        $currencyRates = CurrencyRate::all();
        $userSales = UserSales::WhereNull('cashback_egp')->orderByDesc('id')->take(2000)->get();
        foreach ($userSales as $userSale) {
            $egpRate = $this->getCurrencyRate($currencyRates, 'EGP', $userSale->transaction_time);
            $sarRate = $this->getCurrencyRate($currencyRates, 'SAR', $userSale->transaction_time);
            $aedRate = $this->getCurrencyRate($currencyRates, 'AED', $userSale->transaction_time);
            $kwdRate = $this->getCurrencyRate($currencyRates, 'KWD', $userSale->transaction_time);
            $userSale->cashback_egp = $egpRate * $userSale->cashback;
            $userSale->cashback_sar = $sarRate * $userSale->cashback;
            $userSale->cashback_aed = $aedRate * $userSale->cashback;
            $userSale->cashback_kwd = $kwdRate * $userSale->cashback;
            $userSale->save();
        }
        $userBonuses = UserBonus::where('amount_egp',0)->orWhereNull('amount_egp')->orderByDesc('id')->take(2000)->get();
        foreach ($userBonuses as $userBonus) {
            $egpRate = $this->getCurrencyRate($currencyRates, 'EGP', $userBonus->awarded_on);
            $sarRate = $this->getCurrencyRate($currencyRates, 'SAR', $userBonus->awarded_on);
            $aedRate = $this->getCurrencyRate($currencyRates, 'AED', $userBonus->awarded_on);
            $kwdRate = $this->getCurrencyRate($currencyRates, 'KWD', $userBonus->awarded_on);
            $userBonus->amount_egp = $egpRate * $userBonus->amount;
            $userBonus->amount_sar = $sarRate * $userBonus->amount;
            $userBonus->amount_aed = $aedRate * $userBonus->amount;
            $userBonus->amount_kwd = $kwdRate * $userBonus->amount;
            $userBonus->save();
        }
        $userNewBonuses = UserNewBonus::where('amount_egp',0)->orWhereNull('amount_egp')->orderByDesc('id')->take(2000)->get();
        foreach ($userNewBonuses as $userNewBonus) {
            $egpRate = $this->getCurrencyRate($currencyRates, 'EGP', $userNewBonus->awarded_on);
            $sarRate = $this->getCurrencyRate($currencyRates, 'SAR', $userNewBonus->awarded_on);
            $aedRate = $this->getCurrencyRate($currencyRates, 'AED', $userNewBonus->awarded_on);
            $kwdRate = $this->getCurrencyRate($currencyRates, 'KWD', $userNewBonus->awarded_on);
            $userNewBonus->amount_egp = $egpRate * $userNewBonus->amount;
            $userNewBonus->amount_sar = $sarRate * $userNewBonus->amount;
            $userNewBonus->amount_aed = $aedRate * $userNewBonus->amount;
            $userNewBonus->amount_kwd = $kwdRate * $userNewBonus->amount;
            $userNewBonus->save();
        }
        $userPayouts = UserPayouts::whereNull('amount_egp')->orderByDesc('id')->take(2000)->get();
        foreach ($userPayouts as $userPayout) {
            $egpRate = $this->getCurrencyRate($currencyRates, 'EGP', $userPayout->created_at);
            $sarRate = $this->getCurrencyRate($currencyRates, 'SAR', $userPayout->created_at);
            $aedRate = $this->getCurrencyRate($currencyRates, 'AED', $userPayout->created_at);
            $kwdRate = $this->getCurrencyRate($currencyRates, 'KWD', $userPayout->created_at);
            $userPayout->amount_egp = $egpRate * $userPayout->amount;
            $userPayout->amount_sar = $sarRate * $userPayout->amount;
            $userPayout->amount_aed = $aedRate * $userPayout->amount;
            $userPayout->amount_kwd = $kwdRate * $userPayout->amount;
            $userPayout->save();
        }
        $endTime = microtime(true);
        $executionTime = $endTime - $startTime;
        Log::channel("separated_file")->info("The Script".class_basename($this)."   Took : $executionTime");
    }

    public function getCurrencyRate($currencyRateCollection, $currencyCode, $dateTime)
    {
        $currencyRate = $currencyRateCollection->where('start_date', '<', $dateTime)
            ->where('currency_code', $currencyCode)
            ->sortByDesc('start_date')->first();
        if (is_null($currencyRate)) $currencyRate = $currencyRateCollection->where('currency_code', $currencyCode)->first();
        return $currencyRate->conversion_rate;
    }
}
