<?php

namespace App\Observers\TodayIssue;

use App\Models\BadDebts\BadDebt;
use App\Services\TodayIssueObserverService;
use Illuminate\Support\Facades\Log;

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

    public function created(BadDebt $badDebt): void
    {
        $this->safeExecute(fn () => $this->service->handleBadDebtChange($badDebt));
    }

    public function updated(BadDebt $badDebt): void
    {
        $this->safeExecute(fn () => $this->service->handleBadDebtChange($badDebt));
    }

    public function deleted(BadDebt $badDebt): void
    {
        $this->safeExecute(fn () => $this->service->handleBadDebtDeleted($badDebt));
    }

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