<?php

namespace App\Providers;

use App\Services\SettingsService;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Log;

class ThemeServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap services.
     */
    public function boot(): void
    {
        try {
            // Only run if not in console and database is accessible
            if (!$this->app->runningInConsole() && $this->canAccessDatabase()) {
                
                // Share ALL settings with ALL views
                View::composer('*', function ($view) {
                    try {
                        if (Schema::hasTable('settings')) {
                            $settingsService = app(SettingsService::class);
                            
                            // Share all settings with the view
                            $view->with('themeVariables', $settingsService->getThemeCssVariables());
                            $view->with('themeMode', $settingsService->getThemeMode());
                            $view->with('appSettings', $settingsService->getAll());
                            
                            // Share individual hotel settings for easy access
                            $view->with('hotelName', $settingsService->getHotelName());
                            $view->with('hotelAddress', $settingsService->getHotelAddress());
                            $view->with('hotelPhone', $settingsService->getHotelPhone());
                            $view->with('hotelEmail', $settingsService->getHotelEmail());
                            $view->with('vatRate', $settingsService->getVatRate());
                            $view->with('currencySymbol', $settingsService->getCurrencySymbol());
                        }
                    } catch (\Exception $e) {
                        Log::warning('Could not load theme in view composer: ' . $e->getMessage());
                    }
                });
                
                // Register custom Blade directives
                Blade::directive('themeStyles', function () {
                    return "<?php echo '<style>' . app(App\Services\SettingsService::class)->getThemeStyles() . '</style>'; ?>";
                });
                
                // This is the @themeJson directive you were using
                Blade::directive('themeJson', function () {
                    return "<?php 
                        \$settingsService = app(App\Services\SettingsService::class);
                        echo '<script>window.themeSettings = ' . json_encode([
                            'variables' => \$settingsService->getThemeCssVariables(),
                            'mode' => \$settingsService->getThemeMode(),
                            'hotelName' => \$settingsService->getHotelName()
                        ]) . ';</script>';
                    ?>";
                });
                
                // This is the @hotelSettings directive with all settings
                Blade::directive('hotelSettings', function () {
                    return "<?php 
                        \$settingsService = app(App\Services\SettingsService::class);
                        echo '<script>window.hotelSettings = ' . json_encode([
                            'name' => \$settingsService->getHotelName(),
                            'address' => \$settingsService->getHotelAddress(),
                            'phone' => \$settingsService->getHotelPhone(),
                            'email' => \$settingsService->getHotelEmail(),
                            'vat_rate' => \$settingsService->getVatRate(),
                            'currency' => \$settingsService->getCurrencySymbol(),
                            'currency_position' => \$settingsService->getCurrencyPosition(),
                            'theme' => [
                                'variables' => \$settingsService->getThemeCssVariables(),
                                'mode' => \$settingsService->getThemeMode()
                            ]
                        ]) . ';</script>';
                    ?>";
                });
                
                Log::info('ThemeServiceProvider booted successfully');
            }
        } catch (\Exception $e) {
            Log::error('ThemeServiceProvider error: ' . $e->getMessage());
        }
    }
    
    protected function canAccessDatabase()
    {
        try {
            \DB::connection()->getPdo();
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }
}