<?php

namespace App\Console\Commands\Cashback;

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

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Handle expiring 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(' (DATEDIFF(expires_on , CURRENT_DATE )) IN (1,5,11) ')->get();

        foreach ($bonuses as $bonus) {
            if ($bonus->user)
                event(new BonusExpireSoon($bonus->user, $bonus));
        }
    }
}
