<?php

namespace App\Providers;

use App\Http\Middleware\ApiKeyMiddleware;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

// Use this class


class RouteServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     */
    public const HOME = '/home';
    protected $namespace = 'App\Http\Controllers\api\v1';

    public function boot(): void
    {
        RateLimiter::for(
            'api',
            function (Request $request) {
                return Limit::perMinute(100)->by($request->user()?->id ?: $request->ip());
            }
        );

        // deploy route has no ApiKeyMiddleware — guarded by X-Deploy-Secret header instead
        $this->deployRoutes();

        Route::middleware([ApiKeyMiddleware::class])
            ->group(
                function () {
                    $this->adminAccountRoutes();
                    $this->adminCategoryRoutes();
                    $this->adminSubCategoryRoutes();
                    $this->adminVendorRoutes();
                    $this->adminBankRoutes();
                    $this->adminBlogRoutes();
                    $this->adminBlogCategoryRoutes();
                    $this->adminServiceItemRoutes();
                    $this->userBlogRoutes();
                    $this->userBlogCategoryRoutes();
                    $this->userAccountRoutes();
                    $this->userOrderRoutes();
                    $this->userVendorRoutes();
                    $this->userVendorServiceRoutes();
                    $this->userPackageRoutes();
                    $this->systemSettingsRoutes();
                    $this->userBankRoutes();
                    $this->bankRoutes();
                    $this->blogRoutes();
                    $this->authRoutes();
                    $this->vendorServiceItemRoutes();
                    $this->userServiceItemRoutes();
                }
            );
    }



    public function deployRoutes(): void
    {
        Route::prefix('api/v1')
            ->namespace($this->namespace)
            ->group(function () {
                include base_path('routes/v1/deploy.php');
            });
    }

    public function adminCategoryRoutes(): void
    {
        Route::middleware('admins')
            ->prefix('api/v1/admin/category')
            ->namespace($this->namespace)
            ->group(
                function () {
                    include base_path('routes/v1/Admin/category.php');
                }
            );
    }

    public function adminSubCategoryRoutes(): void
    {
        Route::middleware('admins')
            ->prefix('api/v1/admin/subcategory')
            ->namespace($this->namespace)
            ->group(
                function () {
                    include base_path('routes/v1/Admin/subcategory.php');
                }
            );
    }

    public function adminVendorRoutes(): void
    {
        Route::middleware('admins')
            ->prefix('api/v1/admin/vendor')
            ->namespace($this->namespace)
            ->group(
                function () {
                    include base_path('routes/v1/Admin/vendor.php');
                }
            );
    }

    public function userBlogRoutes(): void
    {
        Route::middleware('api')
            ->prefix('api/v1/user/blog')
            ->namespace($this->namespace)
            ->group(
                function () {
                    include base_path('routes/v1/User/blog.php');
                }
            );
    }

    public function userBlogCategoryRoutes(): void
    {
        Route::middleware('api')
            ->prefix('api/v1/user/blogcategory')
            ->namespace($this->namespace)
            ->group(
                function () {
                    include base_path('routes/v1/User/blogcategory.php');
                }
            );
    }

    public function userAccountRoutes(): void
    {
        Route::middleware('user.auth')
            ->prefix('api/v1/user/account')
            ->namespace($this->namespace)
            ->group(
                function () {
                    include base_path('routes/v1/User/account.php');
                }
            );
    }

    public function userOrderRoutes(): void
    {
        Route::middleware('api')
            ->prefix('api/v1/user')
            ->namespace($this->namespace)
            ->group(
                function () {
                    include base_path('routes/v1/User/order.php');
                }
            );
    }

    public function userVendorRoutes(): void
    {
        Route::middleware('api')
            ->prefix('api/v1/user')
            ->namespace($this->namespace)
            ->group(
                function () {
                    include base_path('routes/v1/User/vendor.php');
                }
            );
    }

    public function userPackageRoutes(): void
    {
        Route::middleware('api')
            ->prefix('api/v1/user')
            ->namespace($this->namespace)
            ->group(
                function () {
                    include base_path('routes/v1/User/package.php');
                }
            );
    }

    public function adminAccountRoutes(): void
    {
        Route::middleware('admins')
            ->prefix('api/v1/admin')
            ->namespace($this->namespace)
            ->group(
                function () {
                    include base_path('routes/v1/Admin/account.php');
                }
            );
    }

    public function systemSettingsRoutes(): void
    {
        Route::middleware('admins')
            ->prefix('api/v1/admin/system-settings')
            ->namespace($this->namespace)
            ->group(
                function () {
                    include base_path('routes/v1/Admin/systemsettings.php');
                }
            );
    }

    public function userBankRoutes(): void
    {
        Route::middleware('user.auth')
            ->prefix('api/v1/user/bank')
            ->namespace($this->namespace)
            ->group(
                function () {
                    include base_path('routes/v1/User/bank.php');
                }
            );
    }

    public function userVendorServiceRoutes(): void
    {
        Route::middleware('vendor')
            ->prefix('api/v1/vendor/service')
            ->namespace($this->namespace)
            ->group(
                function () {
                    include base_path('routes/v1/Vendor/service.php');
                }
            );
    }

    public function bankRoutes(): void
    {
        Route::middleware('api')
            ->prefix('api/v1/bank')
            ->namespace($this->namespace)
            ->group(
                function () {
                    include base_path('routes/v1/bank.php');
                }
            );
    }

    public function adminBankRoutes(): void
    {
        Route::middleware('admin')
            ->prefix('api/v1/admin/bank')
            ->namespace($this->namespace)
            ->group(
                function () {
                    include base_path('routes/v1/Admin/bank.php');
                }
            );
    }

    public function adminBlogRoutes(): void
    {
        Route::middleware('admin')
            ->prefix('api/v1/admin/blog')
            ->namespace($this->namespace)
            ->group(
                function () {
                    include base_path('routes/v1/Admin/blog.php');
                }
            );
    }

    public function adminBlogCategoryRoutes(): void
    {
        Route::middleware('admin')
            ->prefix('api/v1/admin/blogcategory')
            ->namespace($this->namespace)
            ->group(
                function () {
                    include base_path('routes/v1/Admin/blogcategory.php');
                }
            );
    }

    public function adminServiceItemRoutes(): void
    {
        Route::middleware('admin')
            ->prefix('api/v1/admin/serviceitem')
            ->namespace($this->namespace)
            ->group(
                function () {
                    include base_path('routes/v1/Admin/serviceitem.php');
                }
            );
    }

    public function blogRoutes(): void
    {
        Route::middleware('api')
            ->prefix('api/v1/blog')
            ->namespace($this->namespace)
            ->group(
                function () {
                    include base_path('routes/v1/blog.php');
                }
            );
    }

    public function authRoutes(): void
    {
        Route::middleware('api')
            ->prefix('api/v1/auth')
            ->namespace($this->namespace)
            ->group(
                function () {
                    include base_path('routes/v1/auth.php');
                }
            );
    }

    public function vendorServiceItemRoutes(): void
    {
        Route::middleware('vendor')
            ->prefix('api/v1/vendor/serviceitem')
            ->namespace($this->namespace)
            ->group(
                function () {
                    include base_path('routes/v1/Vendor/serviceitem.php');
                }
            );
    }

    public function userServiceItemRoutes(): void
    {
        Route::middleware('user.auth')
            ->prefix('api/v1/user/serviceitem')
            ->namespace($this->namespace)
            ->group(
                function () {
                    include base_path('routes/v1/User/serviceitem.php');
                }
            );
    }
}
