<?php

namespace App\Observers\TodayIssue;

use App\Models\Orders\Order;
use App\Services\TodayIssueObserverService;
use Illuminate\Support\Facades\Log;

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

    public function created(Order $order): void
    {
        $this->safeExecute(fn () => $this->service->handleOrderChange($order));
    }

    public function updated(Order $order): void
    {
        $this->safeExecute(fn () => $this->service->handleOrderChange($order));
    }

    public function deleted(Order $order): void
    {
        $this->safeExecute(fn () => $this->service->handleOrderDeleted($order));
    }

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