<?php

namespace App\Models;

use App\Traits\Searchable;
use Godruoyi\Snowflake\Snowflake;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Department extends Model
{
    use SoftDeletes, Searchable;

    protected $fillable = [
        'reference',
        'name',
        'description'
    ];

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            $model->reference = (new Snowflake)->id();
            $model->created_by = Auth::User()->id;
        });
    }

    /**
     * @return BelongsTo
     */
    public function creator()
    {
        return $this->belongsTo(User::class, 'created_by', 'id')
            ->select('id', 'name')
            ->withTrashed();
    }
}
