<?php

namespace App\Http\Controllers;

use App\Mail\MailVerifyAccount;
use App\Repositories\UserRepository;
use App\User;
use Illuminate\Support\Facades\Mail;


class CronController extends Controller
{


    protected $userRepo;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(UserRepository $userRepo)
    {
        $this->userRepo = $userRepo;

        // $this->middleware('auth');
    }

    public function index()
    {
        $this->checkConfirmation();
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function checkConfirmation()
    {
        $now = date('Y-m-d H:i:s');
        $next = date('Y-m-d H:i:s', strtotime('+3 week'));

        $users = User::where('confirmed', '=', 0)->where('confirmation_code_to', '<', $now)->get();

        foreach ($users as $user) {

            //delete user
            if ($user->confirmation_code_remider == 1) {
                $this->userRepo->deleteUser($user);

            }
            //send new remider
            if ($user->confirmation_code_remider == 0) {
                Mail::to($user->email)->send(new MailVerifyAccount($user->confirmation_code, $user));
                $user->confirmation_code_to = $next;
                $user->confirmation_code_remider = 1;
                $user->save();
            }
        }


        die("okay");

    }

}