<?php

namespace App\Observers\TodayIssue;

use App\Models\Tenants\ExpectedExpense;
use App\Services\TodayIssueObserverService;
use Illuminate\Support\Facades\Log;

/**
 * ExpectedExpense 모델의 TodayIssue Observer
 */
class ExpectedExpenseIssueObserver
{
    public function __construct(
        protected TodayIssueObserverService $service
    ) {}

    public function created(ExpectedExpense $expense): void
    {
        $this->safeExecute(fn () => $this->service->handleExpectedExpenseChange($expense));
    }

    public function updated(ExpectedExpense $expense): void
    {
        $this->safeExecute(fn () => $this->service->handleExpectedExpenseChange($expense));
    }

    public function deleted(ExpectedExpense $expense): void
    {
        $this->safeExecute(fn () => $this->service->handleExpectedExpenseDeleted($expense));
    }

    protected function safeExecute(callable $callback): void
    {
        try {
            $callback();
        } catch (\Throwable $e) {
            Log::warning('TodayIssue ExpectedExpenseObserver failed', [
                'error' => $e->getMessage(),
            ]);
        }
    }
}
