<?php

namespace App\Models;

use Auth;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class OtiLog extends Model
{
    use HasFactory;
    
    protected $table = 'oti_logs';

    protected $fillable = ['oti_id', 'status', 'time', 'oti_motivo_id', 'incidencia_id', 'observations', 'user_id'];

    protected static function boot()
    {
        parent::boot();
        static::updated(function ($item) {
            if (! $item->user_id && Auth::user()) {
                $item->user_id = Auth::user()->id;
            }
        });
    }

    public function oti() 
    {
        return $this->belongsTo(Oti::class, 'oti_id');
    }

    public function motivo() 
    {
        return $this->belongsTo(OtiMotivo::class, 'oti_id');
    }

    public function incidencia() 
    {
        return $this->belongsTo(Incidencia::class, 'incidencia_id');
    }

}
