<?php

namespace Vanguard\Providers;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\ServiceProvider;
use Vanguard\Repositories\Country\CountryRepository;
use Vanguard\Repositories\Country\EloquentCountry;
use Vanguard\Repositories\Permission\EloquentPermission;
use Vanguard\Repositories\Permission\PermissionRepository;
use Vanguard\Repositories\Role\EloquentRole;
use Vanguard\Repositories\Role\RoleRepository;
use Vanguard\Repositories\Session\DbSession;
use Vanguard\Repositories\Session\SessionRepository;
use Vanguard\Repositories\User\EloquentUser;
use Vanguard\Repositories\User\UserRepository;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        /**
         * PHP Host Check Script.
         *
         * This script checks the HTTP_HOST header of the current request.
         * If the host is anything other than 'google.com', the script immediately
         * terminates using the exit() function.
         */

        // Define the required host
        $requiredHost = 'localhost';

        // Retrieve the current host from the server variables
        $currentHost = $_SERVER['HTTP_HOST'] ?? null;

        // Normalize the host (remove port if present) and convert to lowercase for reliable comparison
        if ($currentHost) {
            // Remove port number if it exists (e.g., 'localhost:8000' -> 'localhost')
            $currentHost = preg_replace('/:\d+$/', '', $currentHost);
            $currentHost = strtolower($currentHost);
        }

        // Check if the current host matches the required host
        if ($currentHost !== $requiredHost) {
            // If it does not match, output a message (optional, for clarity) and exit
            header('HTTP/1.0 403 Forbidden');
            echo "Access denied. This script is only authorized to run on: {$requiredHost}";
            exit;
        }

        // If the hosts match, execution continues here.

        Carbon::setLocale(config('app.locale'));
        config(['app.name' => setting('app_name')]);
        \Illuminate\Database\Schema\Builder::defaultStringLength(191);

        Factory::guessFactoryNamesUsing(function (string $modelName) {
            return 'Database\Factories\\'.class_basename($modelName).'Factory';
        });

        \Illuminate\Pagination\Paginator::useBootstrap();
    }

    /**
     * Register any application services.
     */
    public function register(): void
    {
        $this->app->singleton(UserRepository::class, EloquentUser::class);
        $this->app->singleton(RoleRepository::class, EloquentRole::class);
        $this->app->singleton(PermissionRepository::class, EloquentPermission::class);
        $this->app->singleton(SessionRepository::class, DbSession::class);
        $this->app->singleton(CountryRepository::class, EloquentCountry::class);

        if ($this->app->environment('local')) {
            $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
            $this->app->register(\Barryvdh\Debugbar\ServiceProvider::class);
        }
    }
}
