<?php

namespace App\Nova\Resources\Order\Loan\Concerns;

use App\Models\Branch\Branch;
use App\Repos\Order\OrderRepo;
use Illuminate\Database\Eloquent\Model;
use Laravel\Nova\Http\Requests\NovaRequest;

class LoanOrderNovaRepo
{
    public static function fillUniqueId($request, $model): string
    {
        return mb_strtoupper(sprintf(
            '%s-%s',
            Branch::find($request->branch_id)->unique_code ?? 'TB',
            $model->id
        )) ?? uniqid();
    }

    /**
     * After model has been created
     */
    public static function afterCreate(NovaRequest $request, Model $model): void
    {
        $model->update([
            'unique_id' => static::fillUniqueId($request, $model),
            'filled_by' => auth()->id(),
            'user_id' => auth()->id(),
            'status' => OrderRepo::defaultStatus(),
        ]);
    }
}
