<?php

namespace App\Models;

use App\Models\Concerns\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class UserWallet extends Model
{
    use BelongsToTenant;

    protected $fillable = [
        'tenant_id',
        'user_id',
        'currency',
        'balance',
        'status',
        'virtual_account_provider',
        'virtual_account_bank_name',
        'virtual_account_bank_code',
        'virtual_account_number',
        'virtual_account_name',
        'virtual_account_reference',
        'virtual_account_customer_code',
        'virtual_account_status',
        'virtual_account_assigned_at',
        'virtual_account_metadata',
    ];

    protected function casts(): array
    {
        return [
            'balance' => 'decimal:2',
            'virtual_account_assigned_at' => 'datetime',
            'virtual_account_metadata' => 'array',
        ];
    }

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

    public function transactions(): HasMany
    {
        return $this->hasMany(UserWalletTransaction::class);
    }

    public function billPayments(): HasMany
    {
        return $this->hasMany(BillPayment::class);
    }

    public function hasActiveVirtualAccount(): bool
    {
        return $this->virtual_account_status === 'active' && filled($this->virtual_account_number);
    }
}
