<?php

namespace App\Modules\Bookings;

use Illuminate\Support\Facades\Auth;
use App\Models\User;
use App\Modules\Bookings\Device;

class Repair extends Task
{
    protected static $rules = [
        'title'           => 'required',
        'problem'         => 'required',
        'client_id'       => 'required',
        'severity_id'     => 'required',
        'delivered_by'    => 'required_if:needs_delivery,1',
        'return_id'       => 'nullable|exists:tasks,id',
    ];
    
    /**
     * The "booting" method of the model.
     *
     * @return void
     */
    public static function boot()
    {
        parent::boot();

        // When a transaction is created we want to
        // update the created_by field
        static::creating(function ($model) {
            // Clean Summernote content before validation
            if (isset($model->problem)) {
                $cleanProblem = strip_tags($model->problem);
                $cleanProblem = trim($cleanProblem);
                
                // If after stripping HTML tags we have empty content, set to empty string
                if (empty($cleanProblem)) {
                    $model->problem = '';
                }
            }
            
            $model->type = 'repair';
            if ($model->return_id == '') {
                $model->return_id = null;
            }
            if ($model->booking_fee_id == '') {
                $model->booking_fee_id = null;
            }
            $model->created_by = Auth::user()->id;
            $model->updated_by = Auth::user()->id;
        });

        // When a transaction gets updated we want to know who
        // updated it
        static::updating(function ($model) {
            // Clean Summernote content before validation
            if (isset($model->problem)) {
                $cleanProblem = strip_tags($model->problem);
                $cleanProblem = trim($cleanProblem);
                
                // If after stripping HTML tags we have empty content, set to empty string
                if (empty($cleanProblem)) {
                    $model->problem = '';
                }
            }
            
            if ($model->return_id == '') {
                $model->return_id = null;
            }
            if ($model->booking_fee_id == '') {
                $model->booking_fee_id = null;
            }
            $model->updated_by = Auth::user()->id;
        });
    }

    /**
     * Restrict query to type repair.
     */
    public function scopeRepairs($query)
    {
        return $query->where('type', 'repair');
    }

    /**
     * The user who needs to deliver it back.
     *
     * @return \User
     */
    public function delivery_user()
    {
        return $this->belongsTo(User::class, 'delivered_by');
    }

    /**
     * Returns all the devices that have this repair.
     *
     * @return \App\Modules\Bookings\Device
     */
    public function devices()
    {
        return $this->belongsToMany(Device::class, 'task_devices', 'task_id');
    }

    /**
     * Returns the suggested price, calculated based on how long
     * the task was opened for.
     *
     * @return float
     */
    public function getSuggestedPriceAttribute()
    {
        // Retrieve the date when the job changed status to work in progress
        foreach ($this->status_changes()->get() as $v) {
            if ($v->new_status == 4) {
                $status = $v;
                break;
            }
        }

        if (! isset($status)) {
            return 0;
        }

        // Calculate the difference in seconds since the job was assigned.
        $seconds = time() - strtotime($this->created_at);

        if ($seconds <= 0) {
            return 38;
        } elseif ($seconds <= 2 * 60 * 60) {
            // 2 hours

            return 48;
        } elseif ($seconds <= 35 * 60 * 60) {
            return 68;
        } else {
            return 98;
        }
    }
}
