<?php

namespace App\Observers\TodayIssue;

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

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

    public function created(ApprovalStep $step): void
    {
        $this->safeExecute(fn () => $this->service->handleApprovalStepChange($step));
    }

    public function updated(ApprovalStep $step): void
    {
        $this->safeExecute(fn () => $this->service->handleApprovalStepChange($step));
    }

    public function deleted(ApprovalStep $step): void
    {
        $this->safeExecute(fn () => $this->service->handleApprovalStepDeleted($step));
    }

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