<?php

namespace App\Observers;

use App\Models\User;
use App\Services\TenantUserMasterSyncService;
use Illuminate\Support\Facades\Log;

class UserObserver
{
    public function saved(User $user): void
    {
        try {
            app(TenantUserMasterSyncService::class)->sync($user);
        } catch (\Throwable $e) {
            Log::error('UserObserver sync failed', [
                'tenant_user_id' => $user->id,
                'email' => $user->email,
                'error' => $e->getMessage(),
            ]);
        }
    }

    public function deleting(User $user): void
    {
        try {
            app(TenantUserMasterSyncService::class)->delete($user);
        } catch (\Throwable $e) {
            Log::error('UserObserver delete sync failed', [
                'tenant_user_id' => $user->id,
                'email' => $user->email,
                'error' => $e->getMessage(),
            ]);
        }
    }
}
