<?php

namespace kernel\app_modules\user_stage\controllers;

use Exception;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
use kernel\App;
use kernel\app_modules\user_stage\models\forms\CreateUserStageForm;
use kernel\app_modules\user_stage\models\User;
use kernel\app_modules\user_stage\models\UserStage;
use kernel\app_modules\user_stage\notification_messages\UserNewStageNotification;
use kernel\app_modules\user_stage\services\UserStageService;
use kernel\Flash;

class UserStageController extends AdminController
{
    private UserStageService $user_stageService;

    protected function init(): void
    {
        parent::init();
        $this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/user_stage/views/";
        $this->user_stageService = new UserStageService();
    }

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

    #[NoReturn] public function actionAdd(): void
    {
        $user_stageForm = new CreateUserStageForm();
        $user_stageForm->load($_REQUEST);
        if ($user_stageForm->validate()) {
            $user_stage = $this->user_stageService->create($user_stageForm);
            if ($user_stage) {
                $this->redirect("/admin/user_stage/view/" . $user_stage->id);
            }
        }
        $this->redirect("/admin/user_stage/create");
    }

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

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

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

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

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

    /**
     * @throws Exception
     */
    public function actionEdit($id): void
    {
        $user_stage = UserStage::find($id);
        if (!$user_stage) {
            throw new Exception(message: "The user_stage not found");
        }
        $user_stageForm = new CreateUserStageForm();
        $user_stageService = new UserStageService();
        $user_stageForm->load($_REQUEST);
        if ($user_stageForm->validate()) {
            $user_stage = $user_stageService->update($user_stageForm, $user_stage);
            if ($user_stage) {
                $this->redirect("/admin/user_stage/view/" . $user_stage->id);
            }
        }
        $this->redirect("/admin/user_stage/update/" . $id);
    }

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

    #[NoReturn] public function actionSetStage(int $stage_id, int $stage_status, int $user_id): void
    {
        $user = User::find($user_id);
        $stage = UserStage::find($stage_id);

        if ($stage_status === 2) {
            $notification = new UserNewStageNotification($stage, $user);
            App::$notificationDispatcher->dispatch($notification, $user);
            $user->openStage($stage);
        }
        if ($stage_status === 3) {
            $user->completeStage($stage);
        }
        if ($stage_status === 1) {
            $user->closeStage($stage);
        }

        Flash::setMessage('success', 'Этап изменен');
        $this->redirect("/admin/user/view/" . $user_id, 302);
    }
}