<?php

namespace App\Providers;

use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\Rules\Password;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Schema;
use App\Models\Permission;
use App\Models\User;

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

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        $this->configureDefaults();
        $this->configureGates();

        // Override config with Business Settings
        // We wrap this in a try-catch or check if table exists to avoid issues during migration
        try {
             if (Schema::hasTable('business_settings')) {
                $appName = business_config('business_name');
                if ($appName) {
                    config(['app.name' => $appName]);
                }

                // Sync Payment Settings
                config([
                    'services.stripe.key' => business_config('stripe_key'),
                    'services.stripe.secret' => business_config('stripe_secret'),
                    'services.stripe.webhook_secret' => business_config('stripe_webhook_secret'),
                    'services.stripe.mode' => business_config('stripe_mode', 'sandbox'),
                    'services.paypal.client_id' => business_config('paypal_client_id'),
                    'services.paypal.secret' => business_config('paypal_secret'),
                    'services.paypal.mode' => business_config('paypal_mode', 'sandbox'),
                ]);
             }
        } catch (\Exception $e) {
            // Suppress errors during early bootstrap/migration
        }
    }

    protected function configureDefaults(): void
    {
        Date::use(CarbonImmutable::class);

        DB::prohibitDestructiveCommands(
            app()->isProduction(),
        );

        Password::defaults(fn (): ?Password => app()->isProduction()
            ? Password::min(12)
                ->mixedCase()
                ->letters()
                ->numbers()
                ->symbols()
                ->uncompromised()
            : null
        );
    }

    protected function configureGates(): void
    {
        // Super Admin Bypass
        Gate::before(function ($user, $ability) {
            return method_exists($user, 'isSuperAdmin') && $user->isSuperAdmin() ? true : null;
        });

        // Panel Access
        Gate::define('access-admin-panel', fn(User $user) => $user->isAdmin());
        Gate::define('access-customer-panel', fn(User $user) => $user->isCustomer());

        // Dynamic Permissions
        try {
            if (Schema::hasTable('permissions')) {
                foreach (Permission::all() as $permission) {
                    Gate::define($permission->name, function (User $user) use ($permission) {
                        return $user->hasPermission($permission->name);
                    });
                }
            }
        } catch (\Exception $e) {
            // Ignored during migrations/setup
        }
    }
}
