<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

class ActivityLog extends Model
{
    public $timestamps = false;

    protected $fillable = ['action', 'model_type', 'model_id', 'user_id', 'changes', 'created_at'];

    protected $casts = [
        'changes' => 'array',
        'created_at' => 'datetime',
    ];

    public static function record(string $action, Model $model, array $changes = []): void
    {
        static::create([
            'action' => $action,
            'model_type' => class_basename($model),
            'model_id' => $model->getKey(),
            'user_id' => Auth::id(),
            'changes' => empty($changes) ? null : $changes,
            'created_at' => now(),
        ]);
    }

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