<?php

namespace App\Modules\Bookings;

use Illuminate\Support\Facades\Log;
use Exception;

class FollowUpTaskCreator
{
    /**
     * A reference to the task object.
     *
     * @var App\Modules\Bookings\Task
     */
    private $task;
    /**
     * Create a new task to ask to the client about the previous one.
     *
     * @param  App\Modules\Bookings\Task $task
     *
     * @return string | false
     */
    public function create($task)
    {
        try {
            $this->setTask($task);

            // Crerate the new task		
            $overviewTask = Task::create([

                        'title'             => $this->getTitle(),
                        'problem'           => $this->getProblem(),
                        'client_id'         => $this->task->client_id,
                        'client_type'       => $this->task->client_type,
                        'severity_id'       => $this->getSeverity(),
                        'type'              => $this->getType(),
                        'site_id'           => $this->task->site_id,
                        'site_user_id'      => $this->task->site_user_id,
                        'status_id'         => $this->getStatus(),
                        'followup_id'       => $this->task->id,

                    ]);

            if (! $overviewTask->exists) {
                throw new Exception('Failed to create the task');
            }

            //Add the task is being close as a related task in the new followup one
            $overviewTask->related_tasks()->attach([$task->id]);

            return $overviewTask->id;
        } catch (Exception $e) {
            $this->log->error($e);
        }

        return false;
    }

    /**
     * Sets the task.
     *
     * @param \App\Modules\Bookings\Task $task
     */
    public function setTask($task)
    {
        $this->task = $task;
    }

    /**
     * Gets the task.
     *
     * @return \App\Modules\Bookings\Task
     */
    public function getTask()
    {
        return $this->task;
    }

    /**
     * Gets the title of the task.
     *
     * @return string
     */
    public function getTitle()
    {
        return 'Followup on job '.$this->task->id;
    }

    /**
     * Gets the problem of the task.
     *
     * @return string
     */
    public function getProblem()
    {
        return $this->task->user->name.' just finished a task for '.$this->task->client->name.'. Could you please give them a call and be sure that everything is right.';
    }

    /**
     * Get the severity id of the task.
     *
     * @return string
     */
    public function getSeverity()
    {
        //Return a normal severity
        return '2';
    }

    /**
     * Gets the type of the FollowUpTask.
     *
     * @return string
     */
    public function getType()
    {
        return  'followup';
    }

    /**
     * Gets the status id of the FollowUp task.
     *
     * @return string
     */
    public function getStatus()
    {
        //Return status one
        return '1';
    }
}
