<?php

namespace kernel\console\controllers;

use kernel\console\ConsoleController;
use kernel\modules\menu\service\MenuService;
use kernel\modules\option\service\OptionService;
use kernel\modules\user\models\forms\CreateUserForm;
use kernel\modules\user\models\User;
use kernel\modules\user\service\UserService;
use kernel\services\MigrationService;

class AdminConsoleController extends ConsoleController
{

    protected MigrationService $migrationService;
    protected OptionService $optionService;

    protected MenuService $menuService;

    protected UserService $userService;

    public function __construct()
    {
        parent::__construct();
        $this->migrationService = new MigrationService();
        $this->optionService = new OptionService();
        $this->menuService = new MenuService();
        $this->userService = new UserService();
    }

    /**
     * @throws \Exception
     */
    public function actionInit(): void
    {
        $out = $this->migrationService->runAtPath("kernel/modules/menu/migrations");
        $this->out->r("create menu table", "green");

        $out = $this->migrationService->runAtPath("kernel/modules/option/migrations");
        $this->out->r("create option table", "green");

        $out = $this->migrationService->runAtPath("kernel/modules/user/migrations");
        $this->out->r("create user table", "green");

        $out = $this->migrationService->runAtPath("kernel/modules/post/migrations");
        $this->out->r("create post table", "green");

        $out = $this->migrationService->runAtPath("kernel/modules/secure/migrations");
        $this->out->r("create secret_code table", "green");

        $this->optionService->createFromParams(
            key: "admin_theme_paths",
            value: "{\"paths\": [\"{KERNEL_ADMIN_THEMES}\", \"{APP}/admin_themes\"]}",
            label: "Пути к темам админпанели"
        );
        $this->out->r("create option admin_theme_paths", "green");

        $this->optionService->createFromParams(
            key: "active_admin_theme",
            value: "{KERNEL_ADMIN_THEMES}/default",
            label: "Активная тема админпанели"
        );
        $this->out->r("create option active_admin_theme", "green");

        $this->optionService->createFromParams(
            key: "module_paths",
            value: "{\"paths\": [\"{KERNEL_MODULES}\", \"{APP}/modules\"]}",
            label: "Пути к модулям"
        );
        $this->out->r("create option module_paths", "green");

        $this->optionService->createFromParams(
            key: "active_modules",
            value: "{\"modules\":[\"admin_themes\", \"secure\", \"user\", \"menu\", \"post\", \"option\"]}",
            label: "Активные модули"
        );
        $this->out->r("create option active_modules", "green");

        $this->optionService->createFromParams(
            key: "entity_relations",
            value: "{}",
            label: "Свойства сущностей"
        );
        $this->out->r("create option entity_relations", "green");

        $this->menuService->createItem([
            "label" => "Модули",
            "url" => "/admin",
            "slug" => "module",
            "priority" => 1,
        ]);
        $this->out->r("create item menu module", "green");

        $this->menuService->createItem([
            "label" => "Пользователи",
            "url" => "#",
            "slug" => "user",
        ]);
        $this->menuService->createItem([
            "label" => "Список",
            "url" => "/admin/user",
            "slug" => "user_list",
            "parent_slug" => "user",
        ]);
        $this->menuService->createItem([
            "label" => "Создать",
            "url" => "/admin/user/create",
            "slug" => "user_create",
            "parent_slug" => "user",
        ]);
        $this->out->r("create item menu user", "green");

        $this->menuService->createItem([
            "label" => "Посты",
            "url" => "/admin/post",
            "slug" => "post",
        ]);
        $this->out->r("create item menu post", "green");

        $this->menuService->createItem([
            "label" => "Настройки",
            "url" => "#",
            "slug" => "settings",
        ]);
        $this->out->r("create item menu settings", "green");

        $this->menuService->createItem([
            "label" => "Темы админпанели",
            "url" => "/admin/settings/admin-themes",
            "slug" => "admin-themes",
            "parent_slug" => "settings"
        ]);
        $this->out->r("create item menu admin-themes", "green");

        $this->menuService->createItem([
            "label" => "Меню",
            "url" => "/admin/settings/menu",
            "slug" => "menu",
            "parent_slug" => "settings"
        ]);
        $this->out->r("create item menu menu", "green");

        $this->menuService->createItem([
            "label" => "Опции",
            "url" => "/admin/option",
            "slug" => "option"
        ]);
        $this->out->r("create item menu option", "green");

        $user = new CreateUserForm();
        $user->load([
            'username' => 'admin',
            'password' => 'ChangeMe',
            'email' => 'admin@test.ru',
            'role' => User::ADMIN_USER_ROLE,
        ]);
        $this->userService->create($user);
        $this->out->r("create user with username admin", "green");
    }

}