<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // Ensure all database connections use UTF-8 on every connection
        \Illuminate\Support\Facades\DB::connection()->getPdo()->exec("SET NAMES 'utf8mb4' COLLATE 'utf8mb4_unicode_ci'");
        \Illuminate\Support\Facades\DB::connection()->getPdo()->exec("SET CHARACTER SET utf8mb4");
        \Illuminate\Support\Facades\DB::connection()->getPdo()->exec("SET character_set_client=utf8mb4");
        \Illuminate\Support\Facades\DB::connection()->getPdo()->exec("SET character_set_connection=utf8mb4");
        \Illuminate\Support\Facades\DB::connection()->getPdo()->exec("SET character_set_results=utf8mb4");
        
        // Also set on every query for safety
        \Illuminate\Support\Facades\DB::listen(function ($query) {
            try {
                \Illuminate\Support\Facades\DB::connection()->getPdo()->exec("SET NAMES 'utf8mb4' COLLATE 'utf8mb4_unicode_ci'");
                \Illuminate\Support\Facades\DB::connection()->getPdo()->exec("SET character_set_results=utf8mb4");
            } catch (\Exception $e) {
                // Silently fail if connection is not ready
            }
        });
    }
}
