<?php

namespace App\Providers;

use App\Models\Module;
use App\Models\Setting;
use Modules\Notification\Interfaces\ManagementNotificationRepositoryInterface;
use App\Models\Language;;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Schema;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Category\Repositories\CategoryRepository;
use Modules\Category\Services\CategoryService;
use Modules\Notification\Repositories\ManagementNotificationRepository;
use Modules\Notification\Services\ManagementNotificationService;
use Modules\Page\Models\Page;
use Modules\Page\Repositories\PageRepository;
use Modules\Page\Services\PageService;
use Modules\User\Enums\UserRoleEnum;
use Modules\User\Services\AccountService;
use Modules\User\Services\ModuleService;
use Illuminate\Pagination\Paginator;

class AppServiceProvider extends ServiceProvider
{

    /**
     * Register any application orders.
     *
     * @return void
     */

    /**
     * @return null
     */

    public function register()
    {

    }

    /**
     * Bootstrap any application orders.
     *
     * @return void
     */
    public function boot()
    {
        // Fix migrate issue associated with index key length
        Schema::defaultStringLength(191);
        Paginator::useBootstrapFive();

        // Email configuration Got from settings

        if (Schema::hasTable('settings')){
            $settings = Setting::cursor();

            if(count($settings) > 0){
                foreach ($settings as $setting) {
                    $settings->{$setting['key']} = $setting['value'];
                }

                Config::set('app.name', $settings->website_name);
                Config::set('mail.name', $settings->website_name);
                Config::set('mail.driver',$settings->mail_driver);
                Config::set('mail.host',$settings->mail_host);
                Config::set('mail.port',$settings->mail_port);
                Config::set('mail.username',$settings->mail_username);
                Config::set('mail.password',$settings->mail_password);
                Config::set('mail.from', [
                    'address' => $settings->mail_from_address,
                    'name' => $settings->mail_from_name,
                ]);

            }
        }

    }
}
