<?php

namespace App\Providers;

use App\Models\Company;
// use App\Models\RelationshipUserCompany;
use App\Models\User;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\View;

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

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        if ($this->app->environment() == 'production') {
            $this->app['request']->server->set('HTTPS', true);
        }

        $this->setSchemaDefaultLength();

        Validator::extend('base64image', function ($attribute, $value, $parameters, $validator) {
            $explode = explode(',', $value);
            $allow = ['png', 'jpg', 'svg', 'jpeg'];
            $format = str_replace(
                [
                    'data:image/',
                    ';',
                    'base64',
                ],
                [
                    '', '', '',
                ],
                $explode[0]
            );

            // check file format
            if (!in_array($format, $allow)) {
                return false;
            }

            // check base64 format
            if (!preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $explode[1])) {
                return false;
            }

            return true;
        });

        view()->composer('partials.language_switcher', function ($view) {
            $view->with('current_locale', app()->getLocale());
            $view->with('available_locales', config('app.available_locales'));
        });


        View::composer(['theme::user.layouts.app'], function ($view) {
            if (auth()->check()) {
                $current_user = auth()->user();
                $current_company = Company::find($current_user->current_active_company);
                // $userCompanies = RelationshipUserCompany::where('user_id', $current_user->id)->pluck('company_id')->all();
                // $companies = Company::findMany($userCompanies);
                
                $view->with([
                    // 'companies' => $companies,
                    // 'userCompanies' => $userCompanies,
                    'current_company' => $current_company,
                ]);
            }
        });
        // View::composer(['theme::layouts.app'], function ($view) {
        //     if (auth()->check()) {
        //         $current_user = auth()->user();
        //         $users = User::where('role_id', '>=', '11')->where('status', 'active')->get();
                
        //         $view->with([
        //             'users' => $users,
        //         ]);
        //     }
        // });
        

    }

    private function setSchemaDefaultLength(): void
    {
        try {
            Schema::defaultStringLength(191);
        }
        catch (\Exception $exception){}
    }
}
