<?php

namespace App\Modules\Bookings;

use Illuminate\Support\Facades\Auth;

class Onsite extends Task
{
    /**
     * Array of validation rules.
     *
     * @var  array
     */
    protected static $rules = [
        'title'        => 'required',
        'problem'      => 'required',
        'client_id'    => 'required',
        'client_type'  => 'required',
        'severity_id'  => 'required',
        'onsite_start' => 'required',
        'onsite_end'   => 'required',
        'site_id'      => 'required_if:client_type,client',
        'site_user_id' => 'required_if:client_type,client',
    ];

    /**
     * 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) {
            $model->type = 'onsite';
            $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) {
            $model->updated_by = Auth::user()->id;
        });
    }

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