<?php

namespace App\Console\Commands;

use App\Mail\RemindDate;
use App\Models\UserPackage;
use App\User;
use Carbon\Carbon;
use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
//	    $owner = User::where('email', 'huynt57@gmail.com')->first();
//	    \Mail::to($owner)->send(new RemindDate($owner, '111'));

	    $time = Carbon::now()->addDay(5);

	    $owners = User::where('type', User::OWNER)->get();

	    foreach ($owners as $owner)
	    {
	    	$userPackage = UserPackage::where('user_id', $owner->id)->first();
	    	if($userPackage)
		    {
		    	$endDate = $userPackage->end_date;
		    	if($time->greaterThan($endDate))
			    {
			        if(!str_contains($owner->email, 'huynt57')) {
			        	if(!empty($owner->email)) {
					        \Mail::to( $owner )->send( new RemindDate( $owner, $endDate->format( 'd/m/Y' ) ) );
				        }
                    }
			    }
		    }
	    }
    }
}
