<?php

namespace App\Providers;

use App\Models\Setting;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;

/**
 * Memuat setting dari DB & menimpa config() saat runtime, sehingga
 * config('services.duitku.*') / config('ads.default_rate') / config('mail.*')
 * membaca nilai yang diatur admin via UI tanpa mengubah .env.
 */
class SettingServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        //
    }

    public function boot(): void
    {
        try {
            foreach (Setting::allSettings() as $key => $value) {
                if ($value === null || $value === '') {
                    continue;
                }

                config([$key => $value]);
            }
        } catch (\Throwable $e) {
            // Tabel belum ada (fresh DB saat migrate) atau error lain: jangan crash app.
            Log::debug('SettingServiceProvider: skip config override', ['error' => $e->getMessage()]);
        }
    }
}
