<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Account extends Model
{
    use HasFactory;
    protected $guarded = [];
    protected $dates = ['previous_balance_date','created_at','updated_at'];
    protected $hidden = ['pin'];

    public function user(){
        return $this->belongsTo(User::class);
    }

    public function transaction(){

        return $this->hasMany(Transaction::class);
    }

    public static function accountExists($account){
        return Account::where('account_number',$account)->exists();
    }
    public static function generateAccountNumber(){
        $account = mt_rand(1000000000,9999999999);
        if(Self::accountExists($account)){
            return Self::generateAccountNumber();
        }
        return $account;
    }
}
