<?php

namespace kernel\modules\menu\controllers;

use Exception;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
use kernel\FileUpload;
use kernel\models\Menu;
use kernel\modules\menu\models\forms\CreateMenuForm;
use kernel\modules\menu\service\MenuService;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;

class MenuController extends AdminController
{
    private MenuService $menuService;

    protected function init(): void
    {
        parent::init();
        $this->cgView->viewPath = KERNEL_MODULES_DIR . "/menu/views/";
        $this->menuService = new MenuService();
    }

    public function actionCreate(): void
    {
        $this->cgView->render("form.php");
    }

    #[NoReturn] public function actionAdd(): void
    {
        $menuForm = new CreateMenuForm();
        $menuForm->load($_REQUEST);

        if (isset($_FILES['icon_file']) && $_FILES['icon_file']['error'] === UPLOAD_ERR_OK) {
            $file = new FileUpload($_FILES['icon_file'], ['jpg', 'jpeg', 'png']);
            $file->upload();
            $menuForm->setItem('icon_file', $file->getUploadFile());
        }

        if ($menuForm->validate()){
            $menuItem = $this->menuService->create($menuForm);
            if ($menuItem){
                $this->redirect("/admin/settings/menu/view/" . $menuItem->id, code: 302);
            }
        }
        $this->redirect("/admin/settings/menu/create", code: 302);
    }

    /**
     * @throws Exception
     */
    public function actionIndex($page_number = 1): void
    {
        $this->cgView->render("index.php", ['page_number' => $page_number]);
    }

    /**
     * @throws Exception
     */
    public function actionView($id): void
    {
        $menuItem = Menu::find($id);

        if (!$menuItem){
            throw new Exception(message: "The menu item not found");
        }
        $this->cgView->render("view.php", ['menu' => $menuItem]);
    }

    /**
     * @throws RuntimeError
     * @throws SyntaxError
     * @throws LoaderError|Exception
     */
    public function actionUpdate($id): void
    {
        $model = Menu::find($id);
        if (!$model){
            throw new Exception(message: "The menu item not found");
        }

        $this->cgView->render("form.php", ['model' => $model]);
    }

    /**
     * @throws Exception
     */
    public function actionEdit($id): void
    {
        $menuItem = Menu::find($id);
        if (!$menuItem){
            throw new Exception(message: "The menu item not found");
        }

        $menuForm = new CreateMenuForm();
        $menuForm->load($_REQUEST);

        if (isset($_FILES['icon_file']) && $_FILES['icon_file']['error'] === UPLOAD_ERR_OK) {
            $file = new FileUpload($_FILES['icon_file'], ['jpg', 'jpeg', 'png'], $_REQUEST['hashing'] ?? true);
            $file->upload();
            $menuForm->setItem('icon_file', $file->getUploadFile());
        }

        if ($menuForm->validate()){
            $menuItem = $this->menuService->update($menuForm, $menuItem);
            if ($menuItem){
                $this->redirect("/admin/settings/menu/view/" . $menuItem->id, code: 302);
            }
        }
        $this->redirect("/admin/settings/menu/update/" . $id, code: 302);
    }

    #[NoReturn] public function actionDelete($id): void
    {
        Menu::find($id)->delete();
        $this->redirect("/admin/settings/menu/");
    }

}