<?php

namespace kernel\app_modules\card\controllers;

use Exception;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
use kernel\app_modules\card\models\Card;
use kernel\app_modules\card\models\CardTemplate;
use kernel\app_modules\card\models\forms\CreateCardForm;
use kernel\app_modules\card\models\forms\CreateCardTemplateForm;
use kernel\app_modules\card\services\CardService;
use kernel\app_modules\card\services\CardTemplateService;
use kernel\FileUpload;
use kernel\Flash;
use kernel\helpers\Debug;

class CardTemplateController extends AdminController
{

    private CardService $cardService;
    private CardTemplateService $cardTemplateService;
    protected function init(): void
    {
        parent::init();
        $this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/card/views/card_template/";
        $this->cardService = new CardService();
        $this->cardTemplateService = new CardTemplateService();
    }

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

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

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

        if ($cardForm->validate()){
            $cardTemplate = $this->cardTemplateService->create($cardForm);
            if ($cardTemplate){
                $this->redirect("/admin/card_template/" . $cardTemplate->id);
            }
        }

        Flash::setMessage("error", $cardForm->getErrorsStr());
        $this->redirect("/admin/card_template/create");
    }

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

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

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

    /**
     * @throws Exception
     */
    public function actionUpdate($id): void
    {
        $model = CardTemplate::find($id);
        if (!$model){
            throw new Exception(message: "The card template not found");
        }

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

    /**
     * @throws Exception
     */
    public function actionEdit($id): void
    {
        $cardTemplate = CardTemplate::find($id);
        if (!$cardTemplate){
            throw new Exception(message: "The card not found");
        }
        $cardTemplateForm = new CreateCardTemplateForm();
        $cardTemplateForm->load($_REQUEST);

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

        if ($cardTemplateForm->validateForUpdate()) {
            $card = $this->cardTemplateService->update($cardTemplateForm, $cardTemplate);
            if ($card) {
                $this->redirect("/admin/card_template/" . $card->id);
            }
        }
        Flash::setMessage("error", $cardTemplateForm->getErrorsStr());

        $this->redirect("/admin/card_template/update/" . $id);
    }

    #[NoReturn] public function actionDelete($id): void
    {
        $card = CardTemplate::find($id)->first();
        $card->delete();
        $this->redirect("/admin/card_template/");
    }

}