<?php

namespace App\Observers;

use App\Models\WalletTransaction;

class WalletTransactionObserver
{
    /**
     * Handle the WalletTransaction "created" event.
     */
   public function created(WalletTransaction $transaction)
    {
        $wallet = $transaction->wallet;

        if (!$wallet) {
            return;
        }

        if ($transaction->type === 'credit') {
            // Add money to wallet
            $wallet->balance += $transaction->amount;
        }

        if ($transaction->type === 'debit') {
            // Deduct money from wallet
            $wallet->balance -= $transaction->amount;
        }

        $wallet->save();
    }

    /**
     * Handle the WalletTransaction "updated" event.
     */
    public function updated(WalletTransaction $walletTransaction): void
    {
        //
    }

    /**
     * Handle the WalletTransaction "deleted" event.
     */
    public function deleted(WalletTransaction $walletTransaction): void
    {
        //
    }

    /**
     * Handle the WalletTransaction "restored" event.
     */
    public function restored(WalletTransaction $walletTransaction): void
    {
        //
    }

    /**
     * Handle the WalletTransaction "force deleted" event.
     */
    public function forceDeleted(WalletTransaction $walletTransaction): void
    {
        //
    }
}
