<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Modules\Bookings\SaleLead;
use App\Console\Commands\AbstractCommand;
use Carbon\Carbon;

class SaleLeadReminders extends AbstractCommand
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'reminders:sale-lead';

    protected $command_title = 'SaleLeadReminders';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Sends a reminder if a sale lead is getting close to the deadline or if the deadline has passed and the task is still open.';

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

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function fire()
    {
        // Mail::pretend();

        $date = Carbon::now()->addDays(1)->toDateTimeString();

        $this->info('Reading a list of sale leads that are getting close to the deadline or passed the deadline.');
        $tasks = SaleLead::saleLeads()->with('user')->whereNotNull('tasks.assigned_to')->where('completed', 0)->where('onsite_end', '<', $date)->groupBy('assigned_to')->get();

        if (count($tasks) > 0) {
            $this->info('Found '.count($tasks).' sale leads that require notifications.');

            foreach ($tasks as $task) {

                // Exclude computer assistance
                if ($task->client_id == 53 && $task->client_type == 'client') {
                    continue;
                }

                // Skip if the user doesn't have an email
                // or the email address is invalid
                // in certain cases people have email addresses like '1'
                // and we need to ignore those
                if (! $task->user->email || ! filter_var($task->user->email, FILTER_VALIDATE_EMAIL)) {
                    $this->error('Skipping reminder as the user does not have a valid email address for sale lead #'.$task->id);
                    continue;
                }

                // We are now fetching all the sale leads
                // that meet the criteria into a single array
                // Instead of sending a separate email for each sale lead
                // we will put them all in one email and render them in a table
                $user_tasks = SaleLead::saleLeads()->where('assigned_to', $task->assigned_to)->where('completed', 0)->where('onsite_end', '<', $date)->get();

                // Send the reminders
                Mail::send('emails.alerts.sale-lead', ['tasks' => $user_tasks, 'user' => $task->user], function ($message) use ($task) {
                    $message->to($task->user->email)->subject('[Seraph3] A summary of all your sale leads that have passed their deadline')->priority(1);
                });
            }
        } else {
            $this->info('No sale leads were found.');
        }
    }

    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return [

        ];
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return [

        ];
    }

    /**
     * Sends an email for sale leads that are overdue
     * and are still open.
     *
     * @return void
     */
    protected function sendOverdueReminder($task)
    {
        Mail::send('emails.alerts.sale-lead-overdue', ['task' => $task], function ($message) use ($task) {
            $message->to($task->user->email)->subject('[Seraph3] One of your sale leads has passed the deadline')->priority(1);
        });
    }

    /**
     * Sends an email for sale leads that are getting close to the deadline date
     * and are still open.
     *
     * @return void
     */
    protected function sendReminder($task)
    {
        Mail::send('emails.alerts.sale-lead-reminder', ['task' => $task], function ($message) use ($task) {
            $message->to($task->user->email)->subject('[Seraph3] One of your sale leads is getting close to the deadline')->priority(1);
        });
    }
}
