<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
    use App\Models\Wallet;
use App\Models\WalletTransaction;
use Illuminate\Support\Facades\DB;
class TopUp extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'amount',
        'phone',
        'status',
        'pm_type',
        'is_paid',
        'user_id',
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'id' => 'integer',
            'is_paid' => 'boolean',
            'user_id' => 'integer',
        ];
    }

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


// protected static function booted()
// {
//     static::created(function (TopUp $topUp) {

//         if (! $topUp->is_paid) {
//             return;
//         }

//         $wallet = Wallet::where('user_id', $topUp->user_id)
//             ->where('is_active', true)
//             ->first();

//         if (! $wallet) {
//             throw new \Exception('Wallet فعال برای کاربر یافت نشد');
//         }

//         DB::transaction(function () use ($topUp, $wallet) {

//             WalletTransaction::create([
//                 'wallet_id' => $wallet->id,
//                 'user_id'   => $topUp->user_id,
//                 'type'      => 'debit',
//                 'amount'    => $topUp->amount,
//                 'pm_type'   => $topUp->pm_type,
//             ]);

//         });
//     });
// }

}
