<?php

namespace App\Console\Commands\Cashback;

use Illuminate\Console\Command;
use Yajny\EventsYj\Events\BonusExpired;
use Yajny\Member\Models\UserBonus;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Handle expired bonuses';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $bonuses = UserBonus::where('status', 'pending')->with('user', 'type_info')->whereRaw(' expires_on <  CURRENT_DATE ')->get();

        foreach ($bonuses as $bonus) {
            $bonus->update(['status' => 'declined']);
            if ($bonus->user)
                event(new BonusExpired($bonus->user, $bonus));
        }
    }
}
