<?php

namespace App\Observers;

use App\Models\AreaSetting;
use App\Support\LaundryAuditLogger;

class AreaSettingObserver
{
    public function saved(AreaSetting $setting): void
    {
        if ($setting->wasRecentlyCreated) {
            LaundryAuditLogger::areaConfigChanged('area_setting.created', [
                'area_id' => $setting->area_id,
            ]);

            return;
        }

        $changes = $setting->getChanges();
        unset($changes['updated_at']);
        if ($changes === []) {
            return;
        }

        LaundryAuditLogger::areaConfigChanged('area_setting.updated', [
            'area_id' => $setting->area_id,
            'changes' => array_keys($changes),
        ]);
    }

    public function deleted(AreaSetting $setting): void
    {
        LaundryAuditLogger::areaConfigChanged('area_setting.deleted', [
            'area_id' => $setting->area_id,
        ]);
    }
}
