<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

final class Log extends Model
{
    use HasFactory;

    /**
     * Disable the updated_at timestamp for append-only logs.
     */
    public const UPDATED_AT = null;

    /**
     * Fillable attributes for mass assignment.
     */
    protected $fillable = [
        'user_id',
        'session_id',
        'category_id',
        'action',
        'metadata',
    ];

    /**
     * Cast attributes to specific types.
     */
    protected function casts(): array
    {
        return [
            'user_id' => 'integer',
            'session_id' => 'integer',
            'category_id' => 'integer',
            'metadata' => 'array',
        ];
    }

    /**
     * Boot the model and prevent updates and deletes.
     */
    protected static function booted(): void
    {
        self::updating(function (): bool {
            return false;
        });

        self::deleting(function (): bool {
            return false;
        });
    }

    /**
     * Get the user that performed the logged action.
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Get the session associated with this log entry.
     */
    public function session(): BelongsTo
    {
        return $this->belongsTo(Session::class);
    }

    /**
     * Get the category associated with this log entry.
     */
    public function category(): BelongsTo
    {
        return $this->belongsTo(Category::class);
    }
}
