<?php

namespace App\Console\Commands;

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

class SupportUntouchedReminders extends AbstractCommand
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'support:untouched';

    protected $command_title = 'SupportUntouchedReminders';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Sends a reminder to the remote support manager with all the remote support jobs that have not been touched for 72 hours.';

    /**
     * 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()->subDays(1)->toDateTimeString();

        $this->info('Calculating remote support tasks that have not been touched since '.$date);
        $tasks = Support::support()->with('user')->whereNotNull('tasks.assigned_to')->where('completed', 0)->whereIn('subtype_id', [1])->where('updated_at', '<', $date)->get();
        $tasks->sortBy('user.name');

        if (count($tasks) > 0) {
            $untouched = [];

            foreach ($tasks as $task) {

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

                // Check for comments
                if ($task->comments()->where('created_at', '>', $date)->count() > 0) {
                    continue;
                }

                // Check for re-assignments
                if ($task->assigned_changes()->where('created_at', '>', $date)->count() > 0) {
                    continue;
                }

                // Pushing all the untouched tasks onto this array to build the summary
                $untouched[] = $task;
            }

            $this->info('Found '.count($untouched).' remote support jobs that have not been touched in a while.');

            Mail::send('emails.alerts.support-reminder-untouched', ['tasks' => $untouched, 'user' => User::where('email', Config::get('emails.managers.support'))->first()], function ($message) {
                $message->to(Config::get('emails.managers.support'))->cc(Config::get('emails.managers.support_cc'))->subject('[Seraph3] A summary of all the remote support jobs that have not been touched in 72 hours')->priority(1);
            });

            // Send an email to the manager
            $this->info('Sending an email containing a summary to '.Config::get('emails.managers.support').' who is the manager of remote support.');
        } else {
            $this->info('No remote support notifications to be sent out at this moment.');
        }
    }

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

        ];
    }

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

        ];
    }
}
