<?php

namespace App\Models\Monitoring;

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

class Log extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'logs';

    protected $fillable = [
        'level',
        'code',
        'msg',
        'entity',
        'entity_id',
        'trace',
        'ip',
    ];

    protected $appends = ['url'];

    public function getUrlAttribute(): string
    {
        if (!$this->entity || !$this->entity_id) {
            return '';
        }

        return strtolower($this->entity) . 's/' . $this->entity_id . '/edit';
    }

    public static function db(
        string $level,
        string $code,
        string $msg,
        ?string $entity = null,
        ?string $id = null,
        ?string $trace = null
    ) {
        self::create([
            'level' => $level,
            'code' => $code,
            'msg' => $msg,
            'entity' => $entity,
            'entity_id' => $id,
            'trace' => $trace,
            'ip' => request()->ip(),
        ]);
    }
}

