<?php

namespace App\Models;

use App\Models\User\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Note extends Model {
    protected $table = 'notes';
    protected $fillable = [
        'user_id',
        'note',
        'is_primary'
    ];

    protected $casts = [
        'is_primary' => 'boolean'
    ];

    use SoftDeletes;

    public function user() {
        return $this->belongsTo(User::class);
    }

    public function owner() {
        return $this->morphTo();
    }

    protected static function boot() {
        parent::boot();
        static::addGlobalScope('order', function (Builder $builder) {
            $builder->orderBy('created_at', 'desc');
        });
    }
}
