<?php

declare(strict_types=1);

namespace App\Observers;

use App\Models\DberpDictionaryType;
use App\Services\DberpAuditService;

final class DberpDictionaryTypeObserver
{
    public function created(DberpDictionaryType $type): void
    {
        if (! auth()->check()) {
            return;
        }

        $audit = app(DberpAuditService::class);
        $audit->log(
            'dberp.dictionary_type.created',
            $type,
            null,
            $audit->snapshot($type, $type->getFillable()),
        );
    }

    public function updated(DberpDictionaryType $type): void
    {
        if (! auth()->check()) {
            return;
        }

        $fillable = $type->getFillable();

        if (array_intersect($fillable, array_keys($type->getChanges())) === []) {
            return;
        }

        $audit = app(DberpAuditService::class);
        $audit->log(
            'dberp.dictionary_type.updated',
            $type,
            null,
            $audit->snapshot($type, $type->getFillable()),
        );
    }
}
