<?php

namespace apexl\Io\modules\menu;

use apexl\Io\exceptions\RecordNotFoundException;
use apexl\Io\includes\Module;
use apexl\Io\includes\Routes;
use apexl\Io\modules\menu\entities\menuEntity;
use apexl\Io\modules\menu\entities\menuLinkEntity;
use apexl\Io\modules\menu\hooks\registerMenuItems;
use app\vendor\apexl\io\src\Io\interfaces\ProvidesHooksInterface;
use Exception;

class menuModule extends Module implements ProvidesHooksInterface
{
    /**
     * Install method, add default menu items and menu nav items
     * @throws Exception
     */
    public function install(): void
    {
        //create main menu
        $mainMenu = new menuEntity();
        try {
            $mainMenu->loadByName('Main Menu');
        } catch (RecordNotFoundException) {
            $mainMenu->name = "Main Menu";
            $mainMenu->module = "menu";
            $mainMenu->links = [
                (new menuLinkEntity())->setData([
                    "title" => "Home",
                    "routeName" => 'home',
                    "href" => Routes::getRoutePattern('home'),
                    "icon" => "home",
                ]),
            ];
            $mainMenu->store();
        }

        //create default account menu
        $accountMenu = new menuEntity();
        try {
            $accountMenu->loadByName('Account Menu');
        } catch (RecordNotFoundException) {
            $accountMenu->name = "Account Menu";
            $accountMenu->module = "menu";
            $accountMenu->links = [
                (new menuLinkEntity())->setData([
                    "title" => "Settings",
                    "routeName" => 'user.display.settings',
                    "href" => Routes::getRoutePattern('user.display.settings'),
                    "icon" => "cogs",
                ]),
                (new menuLinkEntity())->setData([
                    "title" => "Logout",
                    "routeName" => 'user.logout',
                    "href" => Routes::getRoutePattern('user.display.logout'),
                    "icon" => "sign-out",
                ]),
            ];
            $accountMenu->store();
        }
    }

    public function hooks(): array
    {
        return [
            registerMenuItems::class,
        ];
    }
}
