<?php

namespace App\Modules\Bookings;

use Illuminate\Support\Facades\Auth;
use Exception;

class SaleLead extends Task
{
    protected static $rules = [
        'title'           => 'required',
        'problem'         => 'required',
        'client_id'       => 'required',
    ];

    /**
     * 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) {
            throw new Exception('Sale Leads are disabled');

            $model->type = 'sale_lead';
            $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 sale leads.
     */
    public function scopeSaleLeads($query)
    {
        return $query->where('type', 'sale_lead');
    }

    public function setOnsiteEndAttribute($value)
    {
        $this->attributes['onsite_end'] = date('Y-m-d H:i:s', strtotime($value));
    }
}
