<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

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

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);

        $this->overrideCoreConfigDefaults();
    }

    protected function overrideCoreConfigDefaults()
    {
        $config = config('core_config');

        if (!$config) {
            return;
        }

        // Filter out the 'general.settings' item
        $config = array_values(array_filter($config, function ($item) {
            return !isset($item['key']) || $item['key'] !== 'general.settings';
        }));

        foreach ($config as &$item) {
            if (isset($item['key']) && $item['key'] === 'general.general.locale_settings') {
                if (isset($item['fields'])) {
                    foreach ($item['fields'] as &$field) {
                        if (isset($field['name']) && $field['name'] === 'locale') {
                            $field['default'] = 'pt_BR';
                        }
                    }
                }
            }
        }

        config(['core_config' => $config]);
    }
}
