<?php

namespace App\Repos\Order\Card;

use App\Models\Branch\Branch;
use App\Repos\Order\OrderRepo;
use Closure;

class CardOrderRepo
{
    /**
     * When model is being created
     */
    public static function creating(): Closure
    {
        return function ($model) {
            $model->status = $model->status ?: OrderRepo::defaultStatus();
        };
    }

    /**
     * When model is created
     */
    public static function created(): Closure
    {
        return function ($model) {
            $model->update(['unique_id' => static::fillUniqueId($model)]);
        };
    }

    /**
     * Fill unique id
     */
    public static function fillUniqueId($model): string
    {
        return sprintf('TB%s-%s', Branch::find($model->branch_id)->unique_code, $model->id) ?? uniqid();
    }
}
