<?php

namespace kernel\app_modules\user_custom_fields\controllers;

use Exception;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
use kernel\app_modules\user_custom_fields\models\forms\CreateCustomFieldForm;
use kernel\app_modules\user_custom_fields\models\CustomField;
use kernel\app_modules\user_custom_fields\models\forms\CreateUserCustomValueForm;
use kernel\app_modules\user_custom_fields\models\UserCustomValues;
use kernel\app_modules\user_custom_fields\services\CustomFieldService;
use kernel\app_modules\user_custom_fields\services\UserCustomValuesService;
use kernel\Flash;
use kernel\helpers\Debug;

class UserCustomFieldsController extends AdminController
{
    private CustomFieldService $user_custom_fieldsService;
    protected function init(): void
    {
        parent::init();
        $this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/user_custom_fields/views/";
        $this->user_custom_fieldsService = new CustomFieldService();
    }

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

    #[NoReturn] public function actionAdd(): void
    {
        $user_custom_fieldsForm = new CreateCustomFieldForm();
        $user_custom_fieldsForm->load($_REQUEST);
        if ($user_custom_fieldsForm->validate()){
            $user_custom_fields = $this->user_custom_fieldsService->create($user_custom_fieldsForm);
            if ($user_custom_fields){
                $this->redirect("/admin/custom_field/view/" . $user_custom_fields->id);
            }
        }
        Flash::setMessage("error", $user_custom_fieldsForm->getErrorsStr());
        $this->redirect("/admin/custom_field/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_custom_fields = CustomField::find($id);

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

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

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

    /**
     * @throws Exception
     */
    public function actionEdit($id): void
    {
        $user_custom_fields = CustomField::find($id);
        if (!$user_custom_fields){
            throw new Exception(message: "The user_custom_fields not found");
        }
        $user_custom_fieldsForm = new CreateCustomFieldForm();
        $user_custom_fieldsService = new CustomFieldService();
        $user_custom_fieldsForm->load($_REQUEST);
        if ($user_custom_fieldsForm->validate()) {
            $user_custom_fields = $user_custom_fieldsService->update($user_custom_fieldsForm, $user_custom_fields);
            if ($user_custom_fields) {
                $this->redirect("/admin/custom_field/view/" . $user_custom_fields->id);
            }
        }
        $this->redirect("/admin/custom_field/update/" . $id);
    }

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

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

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

    #[NoReturn] public function actionAddUserCustomValues(): void
    {
        $service = new UserCustomValuesService();
        $form = new CreateUserCustomValueForm();
        $form->load($_REQUEST);

        UserCustomValuesService::deleteByUserAndField($form->getItem('user_id'), $form->getItem('custom_field_id'));

        if ($form->validate()){
            $model = $service->create($form);
            if ($model){
                $this->redirect("/admin/custom_field/user_values");
            }
        }
        Flash::setMessage("error", $form->getErrorsStr());
        $this->redirect("/admin/custom_field/user_values");
    }

    #[NoReturn] public function actionDeleteUserCustomValues($id): void
    {
        $user_custom_values = UserCustomValues::find($id)->first();
        $user_custom_values->delete();
        Flash::setMessage("success", "Запись удалена");
        $this->redirect("/admin/custom_field/user_values");
    }
}