<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Gate;
use App\Models\User;
use App\Enums\Role;
use Illuminate\Support\Facades\URL; 

use Dedoc\Scramble\Scramble;
use Dedoc\Scramble\Support\Generator\OpenApi;
use Dedoc\Scramble\Support\Generator\SecurityScheme;

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

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        Scramble::configure()
        ->withDocumentTransformers(function (OpenApi $openApi) {
            $openApi->secure(
                SecurityScheme::http('bearer')
            );
        });

        if (config('app.env') === 'production') {
            URL::forceScheme('https');  
        }

        Gate::define('viewApiDocs', function (User $user) {
            return $user->role === Role::SUPER_ADMIN;
        });

        Gate::define('super_admin', function (User $user) {
            return $user->role === Role::SUPER_ADMIN;
        });

        Gate::define('admin', function (User $user) {
            return $user->role === Role::ADMIN;
        });

        Gate::define('fasilitator', function (User $user) {
            return $user->role === Role::FASILITATOR;
        });

        Gate::define('petani', function (User $user) {
            return $user->role === Role::PETANI;
        });
    }
}
