<?php

namespace kernel\app_modules\slider\controllers;

use kernel\helpers\Debug;
use kernel\app_modules\slider\models\forms\CreateSliderForm;
use kernel\app_modules\slider\models\Slider;
use kernel\app_modules\slider\services\SliderService;
use Exception;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
use kernel\EntityRelation;
use kernel\Request;

class SliderController extends AdminController
{
    private SliderService $sliderService;
    protected function init(): void
    {
        parent::init();
        $this->cgView->viewPath = KERNEL_MODULES_DIR . "/slider/views/";
        $this->sliderService = new SliderService();
    }

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

    #[NoReturn] public function actionAdd(): void
    {
        $sliderForm = new CreateSliderForm();
        $sliderForm->load($_REQUEST);
        if ($sliderForm->validate()) {
            $slider = $this->sliderService->create($sliderForm);

            $entityRelation = new EntityRelation();
            $entityRelation->saveEntityRelation(entity: "post", model: $slider, request: new Request());

            if ($slider) {
                $this->redirect("/admin/slider/view/" . $slider->id);
            }
        }
        $this->redirect("/admin/slider/create", 302);
    }

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

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

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

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

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

    /**
     * @throws Exception
     */
    public function actionEdit(int $id): void
    {
        $slide = Slider::find($id);
        if (!$slide){
            throw new Exception(message: "The slide not found");
        }
        $sliderForm = new CreateSliderForm();
        $sliderForm->load($_REQUEST);
        if ($sliderForm->validate()) {
            $slide = $this->sliderService->update($sliderForm, $slide);

            $entityRelation = new EntityRelation();
            $entityRelation->saveEntityRelation(entity: "slider", model: $slide, request: new Request());

            if ($slide) {
                $this->redirect("/admin/slider/view/" . $slide->id, 302);
            }
        }
        $this->redirect("/admin/slider/update/" . $id, 302);
    }

    /**
     * @throws Exception
     */
    #[NoReturn] public function actionDelete(int $id): void
    {
        $slide = Slider::find($id)->first();
        if (!$slide){
            throw new Exception(message: "The slide not found");
        }

        $entityRelation = new EntityRelation();
        $entityRelation->deleteEntityRelation(entity: "slider", model: $slide);

        $slide->delete();
        $this->redirect("/admin/slider/", 302);
    }
}