<?php

namespace kernel\app_modules\card;

use kernel\helpers\Debug;
use kernel\Module;
use kernel\modules\menu\service\MenuService;
use kernel\services\ConsoleService;
use kernel\services\MigrationService;

class CardModule extends Module
{

    public MenuService $menuService;
    public ConsoleService $consoleService;
    public MigrationService $migrationService;
    public function __construct()
    {
        $this->menuService = new MenuService();
        $this->consoleService = new ConsoleService();
        $this->migrationService = new MigrationService();
    }

    /**
     * @throws \Exception
     */
    public function init(): void
    {
        $this->migrationService->runAtPath("{KERNEL_APP_MODULES}/card/migrations");

        $this->consoleService->runComposerRequire("dragon-code/card-number");
        $this->consoleService->runComposerRequire("endroid/qr-code");

        $this->menuService->createItem([
            "label" => "Card",
            "url" => "/admin/card",
            "slug" => "card",
        ]);

        $this->menuService->createItem([
            "label" => "Список карт",
            "url" => "/admin/card",
            "slug" => "card_list",
            "parent_slug" => "card"
        ]);

        $this->menuService->createItem([
            "label" => "Шаблоны карт",
            "url" => "/admin/card_template",
            "slug" => "card_template",
            "parent_slug" => "card"
        ]);
    }

    /**
     * @throws \Exception
     */
    public function deactivate(): void
    {
        $this->menuService->removeItemBySlug("card");
        $this->menuService->removeItemBySlug("card_list");
        $this->menuService->removeItemBySlug("card_template");
        $this->migrationService->rollbackAtPath("{KERNEL_APP_MODULES}/card/migrations");
        $this->consoleService->runComposerRemove("dragon-code/card-number");
        $this->consoleService->runComposerRemove("endroid/qr-code");
    }
}