<?php

namespace App;

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;
use Openguru\OpenCartFramework\Telegram\TelegramValidateInitDataMiddleware;
use Openguru\OpenCartFramework\Validator\ValidatorServiceProvider;

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

        return (new Application(Arr::mergeArraysRecursively($defaultConfig, $config)))
            ->withRoutes(fn() => $routes)
            ->withServiceProviders([
                SettingsServiceProvider::class,
                QueryBuilderServiceProvider::class,
                CacheServiceProvider::class,
                RouteServiceProvider::class,
                AppServiceProvider::class,
                TelegramServiceProvider::class,
                ValidatorServiceProvider::class,
                TeleCartPulseServiceProvider::class,
                ImageToolServiceProvider::class,
            ])
            ->withMiddlewares([
                TelegramValidateInitDataMiddleware::class,
            ]);
    }
}
