<?php

namespace kernel\modules\user\service;

use kernel\FormModel;
use kernel\helpers\Debug;
use kernel\modules\user\models\User;

class UserService
{

    public function create(FormModel $form_model): false|User
    {
        $model = User::where("username", $form_model->getItem('username'))->first();
        if ($model){
            return $model;
        }
        $model = new  User();
        $model->username = $form_model->getItem('username');
        $model->email = $form_model->getItem('email');
        $model->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT);
        $model->user_photo = $form_model->getItem('user_photo');
        if ($model->save()){
            return $model;
        }

        return false;
    }

    public function update(FormModel $form_model, User $user): false|User
    {
        $user->username = $form_model->getItem('username');
        $user->email = $form_model->getItem('email');
        if ($form_model->getItem('password')) {
            $user->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT);
        }
        $user->user_photo = $form_model->getItem('user_photo');
        if ($user->save()){
            return $user;
        }

        return false;
    }

    /**
     * @param string $field
     * @param string $value
     * @return mixed
     */
    public static function getByField(string $field, string $value): mixed
    {
        return User::where($field, $value)->first();
    }

    public static function createUsernameArr(): array
    {
        foreach (User::all()->toArray() as $user) {

            $userArr[$user['id']] = $user['username'];
        }
        if (!empty($userArr)) {
            return $userArr;
        }
        return [];
    }

    public static function getAuthUser()
    {
        if (isset($_COOKIE['user_id'])){
            $user = User::where("id", $_COOKIE['user_id'])->first();
            if ($user){
                return $user;
            }
        }

        return false;
    }

    public static function getAuthUsername(): string
    {
        $user = self::getAuthUser();
        if ($user){
            return $user->username;
        }

        return  '';
    }

    public static function getAuthUserId(): string
    {
        $user = self::getAuthUser();
        if ($user){
            return $user->id;
        }

        return  '';
    }

    public static function getAuthUserPhoto(): string|null
    {
        $user = self::getAuthUser();
        if ($user){
            if ($user->user_photo) {
                return $user->user_photo;
            }
        }

        return  null;
    }

    public function getByAccessToken(string $token)
    {
        return $this->getByField("access_token", $token);
    }

    public static function createUserByEmailAndPassword(string $email, string $password): void
    {
        $user = new User();
        $user->email = $email;
        $user->username = $email;
        $user->password_hash = password_hash($password, PASSWORD_DEFAULT);
        $user->save();
    }

}