<?php

namespace App\Providers;

use App\Models\LocalizationManagement\City;
use App\Models\LocalizationManagement\Country;
use App\Models\SecurityManagement\Permission;
use App\Models\SecurityManagement\Role;
use App\Models\OrderManagement\Order;
use App\Models\PaymentManagement\PaymentMethod;
use App\Models\ProductManagement\Product;
use App\Models\LocalizationManagement\State;
use App\Models\StockManagement\Adjustment;
use App\Models\StockManagement\StockRequest;
use App\Models\StockManagement\Importation;
use App\Models\StockManagement\Stock;
use App\Models\PaymentManagement\PaymentTransaction;
use App\Models\UserManagement\Admin;
use App\Models\UserManagement\Customer;
use App\Models\UserManagement\User;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider{

    /**
     * The path to the "home" route for your application.
     *
     * This is used by Laravel authentication to redirect users after login.
     *
     * @var string
     */
    public const HOME = '/';

    /**
     * The controller namespace for the application.
     *
     * When present, controller route declarations will automatically be prefixed with this namespace.
     *
     * @var string|null
     */
//     protected $namespace = 'App\\Http\\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot(){
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));

            Route::middleware('web')
                ->as('admin.')
                ->prefix('admin')
                ->namespace($this->namespace)
                ->group(base_path('routes/admin.php'));


            Route::middleware(['web',"roles"])
                //->as('stock.')
                ->name('admin.stocks.')
                ->prefix('admin')
                ->namespace($this->namespace)
                ->group(base_path('routes/stocks-management/web.php'));

            Route::model("product",Product::class);
            Route::model("order",Order::class);
            Route::model("city",City::class);
            Route::model("country",Country::class);
            Route::model("state",State::class);

            Route::model("stock",Stock::class);
            Route::model("adjustment",Adjustment::class);
            Route::model("demand",StockRequest::class);
            Route::model("importation",Importation::class);

            Route::model("role",Role::class);
            Route::model("permission",Permission::class);
            Route::model("user",User::class);
            Route::model("admin",Admin::class);
            Route::model("customer",Customer::class);
            Route::model("transaction",PaymentTransaction::class);
            Route::model("paymentMethod",PaymentMethod::class);
        });
    }

    /**
     * Configure the rate limiters for the application.
     *
     * @return void
     */
    protected function configureRateLimiting(){
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
        });
    }
}
