<?php

namespace App\Modules\Bookings;

use Illuminate\Support\Facades\Auth;
use App\Models\ValidationModel;
use App\Modules\Bookings\RmaItem;

class Rma extends Task
{
    /**
     * Array of validation rules.
     *
     * @var  array
     */
    protected static $rules = [
        'client_id'    => 'required',
        'client_type'  => '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 = 'rma';
            $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 rma.
     */
    public function scopeRma($query)
    {
        return $query->where('type', 'rma');
    }

    /**
     * Retrieves all rma items.
     *
     * @return Illuminate\Database\Eloquent\Relations\HasMany;
     */
    public function items()
    {
        return $this->hasMany(RmaItem::class, 'task_id');
    }
}
