<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Console\Commands\AbstractCommand;
use App\Modules\Bookings\Task;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;

class TaskAlarmNotification extends AbstractCommand
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'alarm:reminder';

    protected $command_title = 'TaskAlarmReminder';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Sends a reminder email for all the jobs that have an alarm set up for today.';

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

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function fire()
    {
        $dateNow = Carbon::now();
        $date = $dateNow->toDateTimeString();
        $date = Carbon::now()->setTime($dateNow->hour, $dateNow->minute, 0)->toDateTimeString();
        $tasks = Task::where('alarm_date', $date)->get();
	$this->info('Date:' . $date);
        if (count($tasks) > 0) {
            foreach ($tasks as $task) {
                if ($task->user && $task->user->email) {
                    \App\Seraph\Services\GlobalMailerService::send('emails.alerts.task-alarm-reminder', ['task' => $task], function ($message) use ($task) {
                        $message->to($task->user->email)->subject('Alarm for task number '.$task->id)->priority(1);
                    });
                }
            }
            $this->info('['.Carbon::now()->toDateTimeString().'] Email successfully sent! Count: '.count($tasks));
        } else {
            $this->info('['.Carbon::now()->toDateTimeString().'] No alarms found that require notification');
        }
    }
}
