<?php

namespace App\Console\Commands;

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

class PrebookReminderCommand extends AbstractCommand
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'seraph:prebook-reminder';

    /**
     * A friendly name of the command that will be used when loggin messages.
     *
     * @var string
     */
    protected $command_title = 'Prebook Reminders';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Sends a text message to all prebook jobs that need to be brought in tomorrow.';

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

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function fire()
    {
        // Get tomorrows date
        $tomorrow = Carbon::now()->addDays(1)->toDateString();
        $this->info('Looking for prebooks with date: '.$tomorrow);

        // Get a list of repairs that are prebooks with tomorrows date - MODERNIZED
        $tasks = Task::where('type', 'repair')
                    ->where('prebook', 1)
                    ->whereDate('prebook_date', $tomorrow)
                    ->groupBy('client_id', 'client_type')
                    ->get();

        if (count($tasks)) {
            $this->info('Found '.count($tasks).' customers that need to be notified');

            // Open the connection to the gms gateway
            $agi = App::make('GSM_AGI');

            foreach ($tasks as $task) {
                $gateway = rand(1, 2);
                $message = 'Hi %s, this is just a reminder that your computer is due in for repair with us tomorrow. Computer Assistance';

                // Get the client details & mobile number
                if ($task->client_type == 'customer') {
                    $message = sprintf($message, $task->client->name);
                    $number = str_replace(' ', '', $task->client->mobile); // also removes white spaces

                    // Validate the mobile number (it should have 11 digits and no letters or any other symbols) and it needs to start with 07
                    if (strlen($number) != 11 || ! ctype_digit($number) || substr($number, 0, 2) != '07') {
                        continue;
                    }

                    // Send the text message
                    $this->info("Sending a message to: $number");
                    // $this->info("Customer name: ". $message);

                    try {
                        $agi->Command('gsm send sms '.$gateway.' '.$number.' "'.$message.'" 30 '.$task->id);
                    } catch (Exception $e) {
                        $this->error($e->getMessage());
                    }
                }
            }
        } else {
            $this->info('No tasks were found');
        }
    }

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

        ];
    }

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

        ];
    }
}
