<?php

namespace App\Support;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

final class SystemReferenceAssigner
{
    public function assign(Model $model, string $prefix, string $column): Model
    {
        $model->forceFill([
            $column => SystemReferenceGenerator::format($prefix, (int) $model->getKey()),
        ])->save();

        return $model->refresh();
    }

    public function pendingPlaceholder(string $prefix): string
    {
        // Keep under 32 chars for legacy varchar(32) reference columns.
        return 'P'.$prefix.Str::lower(Str::random(12));
    }
}
