<?php

namespace App\Observers;

use App\Commands\Ledgers\CreateLedgerCommand;
use App\Models\Expense;
use Illuminate\Support\Facades\DB;

class ExpenseObserver
{
    /**
     * Handle the Expense "created" event.
     */
    public function created(Expense $expense): void
    {
        DB::transaction(
            callback: function () use ($expense) {
                $branch = $expense->branch;
                //check if client is vatable
                if ($branch->isClientVatable) {
                    // create a ledgers for vatable
                    $data = [
                        'transaction_id' => $expense->transaction_id,
                    ];

                    $payload = new CreateLedgerCommand(data: $data);
                } else {
                    // create a lkedgers for non vatable
                }

                //create cash transaction
            },
            attempts: 2
        );
    }

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

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

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

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