<?php

declare(strict_types=1);

namespace Bastion;

use App\ServiceProviders\AppServiceProvider;
use App\ServiceProviders\SettingsServiceProvider;
use Openguru\OpenCartFramework\Application;
use Openguru\OpenCartFramework\Cache\CacheServiceProvider;
use Openguru\OpenCartFramework\ImageTool\ImageToolServiceProvider;
use Openguru\OpenCartFramework\QueryBuilder\QueryBuilderServiceProvider;
use Openguru\OpenCartFramework\Router\RouteServiceProvider;
use Openguru\OpenCartFramework\Support\Arr;
use Openguru\OpenCartFramework\TeleCartPulse\TeleCartPulseServiceProvider;
use Openguru\OpenCartFramework\Telegram\TelegramServiceProvider;

class ApplicationFactory
{
    public static function create(array $settings): Application
    {
        $defaultConfig = require __DIR__ . '/../configs/app.php';
        $routes = require __DIR__ . '/routes.php';

        $merged = Arr::mergeArraysRecursively($defaultConfig, $settings);

        return (new Application($merged))
            ->withRoutes(fn() => $routes)
            ->withServiceProviders([
                SettingsServiceProvider::class,
                QueryBuilderServiceProvider::class,
                RouteServiceProvider::class,
                AppServiceProvider::class,
                CacheServiceProvider::class,
                TelegramServiceProvider::class,
                TeleCartPulseServiceProvider::class,
                ImageToolServiceProvider::class,
            ]);
    }
}
