<?php

namespace kernel\app_modules\card\controllers;

use Exception;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
use kernel\app_modules\card\models\CardProgram;
use kernel\app_modules\card\models\CardTemplate;
use kernel\app_modules\card\models\forms\CreateCardProgramForm;
use kernel\app_modules\card\models\forms\CreateCardTemplateForm;
use kernel\app_modules\card\services\CardProgramService;
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 CardProgramController extends AdminController
{
    private CardService $cardService;
    private CardProgramService $cardProgramService;

    protected function init(): void
    {
        parent::init();
        $this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/card/views/card_program/";
        $this->cardService = new CardService();
        $this->cardProgramService = new CardProgramService();
    }

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

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

        if ($cardForm->validate()){
            $cardProgram = $this->cardProgramService->create($cardForm);
            if ($cardProgram){
                Flash::setMessage("success", "Card program creates " . $cardProgram->title);
                $this->redirect("/admin/card_program/" . $cardProgram->id);
            }
        }

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

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

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

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

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

        if ($cardProgramForm->validate()) {
            $card = $this->cardProgramService->update($cardProgramForm, $cardProgram);
            if ($card) {
                Flash::setMessage("success", "Card program updated " . $cardProgram->title);
                $this->redirect("/admin/card_program/" . $card->id);
            }
        }
        Flash::setMessage("error", $cardProgramForm->getErrorsStr());

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

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

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

}