<?php

namespace kernel\app_modules\card\controllers;

use Exception;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
use kernel\app_modules\card\models\CardTemplate;
use kernel\app_modules\card\models\CardTransaction;
use kernel\app_modules\card\models\forms\CreateCardTemplateForm;
use kernel\app_modules\card\models\forms\CreateCardTransactionForm;
use kernel\app_modules\card\services\CardService;
use kernel\app_modules\card\services\CardTemplateService;
use kernel\app_modules\card\services\CardTransactionService;
use kernel\FileUpload;
use kernel\Flash;

class CardTransactionController extends AdminController
{
    private CardTransactionService $cardTransactionService;

    protected function init(): void
    {
        parent::init();
        $this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/card/views/card_transaction/";
        $this->cardTransactionService = new CardTransactionService();
    }

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

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

        if ($cardForm->validate()){
            $cardTransaction = $this->cardTransactionService->create($cardForm);
            if ($cardTransaction){
                Flash::setMessage("success", "Card transaction created");
                $this->redirect("/admin/card_transaction/" . $cardTransaction->id);
            }
            Flash::setMessage("error", $this->cardTransactionService->getErrorsString());
            $this->redirect("/admin/card_transaction/create");
        }

        Flash::setMessage("error", $cardForm->getErrorsStr());
        $this->redirect("/admin/card_transaction/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 = CardTransaction::find($id);

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

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

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

    /**
     * @throws Exception
     */
    public function actionEdit($id): void
    {
        $cardTransaction = CardTransaction::find($id);
        if (!$cardTransaction){
            throw new Exception(message: "The card transaction not found");
        }
        $cardTransactionForm = new CreateCardTransactionForm();
        $cardTransactionForm->load($_REQUEST);

        if ($cardTransactionForm->validateForUpdate()) {
            $card = $this->cardTransactionService->update($cardTransactionForm, $cardTransaction);
            if ($card) {
                $this->redirect("/admin/card_transaction/" . $card->id);
            }
        }
        Flash::setMessage("error", $cardTransaction->getErrorsStr());

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

    #[NoReturn] public function actionDelete($id): void
    {
        $card = CardTemplate::where("id", $id)->first();
        if (!$card){
            Flash::setMessage("error", "Card transaction not found");
        }
        else {
            $card->delete();
        }

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

}