<?php

namespace App\Observers\TodayIssue;

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

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

    public function created(Deposit $deposit): void
    {
        Log::info('DepositIssueObserver::created CALLED', [
            'deposit_id' => $deposit->id,
            'tenant_id' => $deposit->tenant_id,
        ]);

        $this->safeExecute(fn () => $this->service->handleDepositCreated($deposit));
    }

    public function deleted(Deposit $deposit): void
    {
        $this->safeExecute(fn () => $this->service->handleDepositDeleted($deposit));
    }

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