<?php

namespace App\Providers;

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

class SettingsServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        //
    }

    public function boot(): void
    {
        $this->loadSocialAuthSettings();
    }

    protected function loadSocialAuthSettings(): void
    {
        if (!$this->app->runningInConsole() || $this->app->runningUnitTests()) {
            try {
                // Load Google settings
                if (Setting::get('google_enabled')) {
                    config([
                        'services.google.client_id' => Setting::get('google_client_id'),
                        'services.google.client_secret' => Setting::get('google_client_secret'),
                        'services.google.redirect' => url('/auth/google/callback'),
                    ]);
                }

                // Load Facebook settings
                if (Setting::get('facebook_enabled')) {
                    config([
                        'services.facebook.client_id' => Setting::get('facebook_client_id'),
                        'services.facebook.client_secret' => Setting::get('facebook_client_secret'),
                        'services.facebook.redirect' => url('/auth/facebook/callback'),
                    ]);
                }

                // Load GitHub settings
                if (Setting::get('github_enabled')) {
                    config([
                        'services.github.client_id' => Setting::get('github_client_id'),
                        'services.github.client_secret' => Setting::get('github_client_secret'),
                        'services.github.redirect' => url('/auth/github/callback'),
                    ]);
                }
            } catch (\Exception $e) {
                // Silently fail if settings table doesn't exist yet
            }
        }
    }
}
