<?php

namespace app\themes\svo\controllers;

use JetBrains\PhpStorm\NoReturn;
use kernel\App;
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\services\CustomFieldService;
use kernel\app_modules\user_custom_fields\services\UserCustomValuesService;
use kernel\app_modules\user_stage\models\User;
use kernel\app_modules\user_stage\models\UserStage;
use kernel\Controller;
use kernel\Flash;
use kernel\helpers\Debug;
use kernel\modules\user\service\UserService;
use kernel\Request;

class MainController extends Controller
{
    protected \kernel\modules\user\models\User $user;

    protected function init(): void
    {
        parent::init();
        $this->cgView->viewPath = APP_DIR . "/themes/svo/views/main/";
        $this->cgView->layout = "lk.php";
        $this->cgView->layoutPath = APP_DIR . "/themes/svo/views/layout/";
        $this->cgView->addVarToLayout("resources", "/resources/themes/svo");

        $user = UserService::getAuthUser();
        if ($user){
            $this->cgView->addVarToLayout("currentUser", $user);
            $this->user = $user;
        }
    }

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

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

    public function actionLk(): void
    {
        $user = \kernel\app_modules\user_stage\models\User::find($this->user->id);
        $stages = UserStage::orderBy('id')->get();
        $user->stages()->sync($stages);

        $firstStage = UserStage::where('slug', 'registraciya')->first();
        if ($user->isClosed($firstStage)){
            $user->openStage($firstStage);
        }

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

    public function actionStage(int $id): void
    {
        $stage = UserStage::find($id);
        $fieldsSlug = explode(", ", $stage->user_fields);
        $fields = CustomField::whereIn("slug", $fieldsSlug)->get();

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

    #[NoReturn] public function actionStageSave(): void
    {
        $request = new Request();
        $stageId = $request->post('stage_id');

        $stage = UserStage::find($stageId);
        $fieldsSlug = explode(", ", $stage->user_fields);
        $fields = CustomField::whereIn("slug", $fieldsSlug)->get();

        $service = new UserCustomValuesService();
        $form = new CreateUserCustomValueForm();

        foreach ($fields as $field){
            /* @var CustomField $field */
            if (isset($request->data[$field->slug])){
                UserCustomValuesService::deleteByUserAndField($this->user->id, $field->id);
                $form->load([
                    'user_id' => $this->user->id,
                    'custom_field_id' => $field->id,
                    'value' => $request->data[$field->slug]
                ]);
                $service->create($form);
            }
        }

        Flash::setMessage("success", "Данные сохранены");
        $this->redirect("/lk/", 302);
    }

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

    public function actionLogin(): void
    {
        $this->cgView->layout = "logreg.php";
        $this->cgView->render("login.php");
    }

    public function actionRegister(): void
    {
        $this->cgView->layout = "logreg.php";
        $this->cgView->render("register.php");
    }
}