<?php
/**
 * @package   Essentials YOOtheme Pro 2.2.16 build 0813.1608
 * @author    ZOOlanders https://www.zoolanders.com
 * @copyright Copyright (C) Joolanders, SL
 * @license   http://www.gnu.org/licenses/gpl.html GNU/GPL
 */

use function YOOtheme\app;
use YOOtheme\Arr;
use ZOOlanders\YOOessentials\Auth\AuthManager;
use ZOOlanders\YOOessentials\Source\SourceService;
use ZOOlanders\YOOessentials\Form\Action\SaveDatabase\SaveDatabaseAction;
use ZOOlanders\YOOessentials\Config\ConfigEncrypter;

return [
    'nodes' => [

        // move SaveToDatabase form action db connections to auths
        '2.0.16' => function ($node) {
            if (!isset($node->props['yooessentials_form'], $node->formid)) {
                return;
            }

            $formConfig = (object) ($node->props['yooessentials_form'] ?? []);
            $actions = $formConfig->after_submit_actions ?? [];

            if (empty($actions)) {
                return;
            }

            /** @var AuthManager $authManager */
            $authManager = app(AuthManager::class);

            /** @var ConfigEncrypter $encrypter */
            $encrypter = app(ConfigEncrypter::class);

            $newauths = [];

            // move database custom connections to db driver
            array_walk($actions, function (object &$action) use (&$newauths, $encrypter) {
                if (($action->type ?? '') !== SaveDatabaseAction::NAME) {
                    return;
                }

                $actionConfig = (array) ($action->props ?? []);
                $keysToMove = ['db_user', 'db_password', 'db_port', 'db_host'];

                $auth = array_fill_keys($keysToMove, null);
                $auth = array_merge($auth, Arr::pick($actionConfig, $keysToMove));

                if (empty(array_filter($auth))) {
                    return;
                }

                foreach ($keysToMove as $key) {
                    unset($actionConfig[$key]);
                }

                $id = sha1(json_encode($auth) ?: uniqid());

                $auth['id'] = $id;
                $auth['driver'] = 'database';
                $auth['db_password'] = $encrypter->encrypt($auth['db_password']);

                $newauths[$id] = $auth;

                $actionConfig['db_connection'] = $id;
                $action->props = (object) $actionConfig;
            });

            $formConfig->after_submit_actions = $actions;
            $node->props['yooessentials_form'] = $formConfig;

            if (!empty($newauths)) {
                $auths = $authManager->authConfigs();
                $auths = array_merge([], $auths, array_values($newauths));

                $authManager->saveAuths($auths);
            }
        },

    ],

    'config' => [

        // move source db connections to auths
        '2.1.0.beta.2' => function ($config) {
            $sources = Arr::get($config, SourceService::SOURCES_CONFIG_KEY, []);

            if (empty($sources)) {
                return $config;
            }

            /** @var ConfigEncrypter */
            $encrypter = app(ConfigEncrypter::class);

            $newauths = [];

            array_walk($sources, function (array &$source) use (&$newauths, $encrypter) {
                if (($source['provider'] ?? '') !== 'database' && ($source['external'] ?? false)) {
                    return;
                }

                $keysToMove = ['db_user', 'db_password', 'db_port', 'db_host'];

                $auth = array_fill_keys($keysToMove, null);
                $auth = array_merge($auth, Arr::pick($source, $keysToMove));

                if (empty(array_filter($auth))) {
                    return;
                }

                foreach ($keysToMove as $key) {
                    unset($source[$key]);
                }

                $id = sha1(json_encode($auth) ?: uniqid());

                $auth['id'] = $id;
                $auth['driver'] = 'database';
                $auth['db_password'] = $encrypter->encrypt($auth['db_password']);

                $newauths[$id] = $auth;

                $source['db_connection'] = $id;
            });

            if (!empty($newauths)) {
                $auths = Arr::get($config, AuthManager::AUTHS_CONFIG_KEY, []);
                $auths = array_merge($auths, array_values($newauths));

                Arr::set($config, AuthManager::AUTHS_CONFIG_KEY, $auths);
            }

            Arr::set($config, SourceService::SOURCES_CONFIG_KEY, $sources);

            return $config;
        },

    ],
];
