<?php

namespace App\Observers\TodayIssue;

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

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

    public function created(Stock $stock): void
    {
        $this->safeExecute(fn () => $this->service->handleStockChange($stock));
    }

    public function updated(Stock $stock): void
    {
        $this->safeExecute(fn () => $this->service->handleStockChange($stock));
    }

    public function deleted(Stock $stock): void
    {
        $this->safeExecute(fn () => $this->service->handleStockDeleted($stock));
    }

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