<?php

namespace App\Concerns;

use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

trait RecordsCreator
{
    public static function bootRecordsCreator(): void
    {
        static::creating(function (Model $model): void {
            if ($model->getAttribute('created_by') === null && auth()->check()) {
                $model->setAttribute('created_by', auth()->id());
            }
        });
    }

    /**
     * @return BelongsTo<User, $this>
     */
    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }
}
