<?php

namespace App\Modules\LoanOrder\Repositories;

use App\Modules\Branch\Interfaces\BelongsToBranch;
use App\Modules\Branch\Models\Branch;
use App\Modules\OrderStatus\Interfaces\HasStatus;
use App\Modules\OrderStatus\Repositories\OrderStatusRepository;
use Closure;

class LoanOrderRepository
{
    /**
     * Satisfiable values
     *
     * @return array<string|null, string>
     */
    public static function satisfiableValues(): array
    {
        return [
            null => '-',
            'satisfiable' => __('Satisfiable'),
            'insufficient' => __('Insufficient'),
            'unknown' => __('Unknown'),
        ];
    }

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

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

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