<?php

namespace App\Observers\TodayIssue;

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

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

    public function created(Withdrawal $withdrawal): void
    {
        Log::info('WithdrawalIssueObserver::created CALLED', [
            'withdrawal_id' => $withdrawal->id,
            'tenant_id' => $withdrawal->tenant_id,
        ]);

        $this->safeExecute(fn () => $this->service->handleWithdrawalCreated($withdrawal));
    }

    public function deleted(Withdrawal $withdrawal): void
    {
        $this->safeExecute(fn () => $this->service->handleWithdrawalDeleted($withdrawal));
    }

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