<?php

namespace App\Models;

use App\Enums\RoutingNumberType;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class RoutingNumber extends Model
{
    protected $fillable = [
        'account_id',
        'type',
        'routing_number',
    ];

    protected function casts(): array
    {
        return [
            'type' => RoutingNumberType::class,
        ];
    }

    public function account(): BelongsTo
    {
        return $this->belongsTo(Account::class);
    }

    public static function generateRoutingNumber(): string
    {
        do {
            $number = str_pad(random_int(0, 999999999), 9, '0', STR_PAD_LEFT);
        } while (static::where('routing_number', $number)->exists());

        return $number;
    }
}
