<?php

namespace Shm\ShmAdmin\AdminRPC;

use Shm\Shm;
use Shm\ShmAdmin\AdminPanel;
use Shm\ShmAdmin\SchemaCollections\SubAccountsSchema;
use Shm\ShmAuth\Auth;
use Shm\ShmRPC\ShmRPC;
use Shm\ShmTypes\Classes\EventClasses\ShmEventPostUpdatePayload;
use Shm\ShmTypes\Classes\EventClasses\ShmEventPreUpdatePayload;
use Shm\ShmTypes\Classes\ShmEventPayload;
use Shm\ShmUtils\I18n;
use Shm\ShmUtils\Response;

class AdminRPCUpdateProfile
{

    public static function rpcChangePassword()
    {
        return ShmRPC::lazy(function () {
            return [
                'type' => Shm::bool(),
                'args' => Shm::structure([
                    'currentPassword' => Shm::string(),
                    'newPassword' => Shm::string(),
                ]),
                'resolve' => function ($root, $args) {
                    Auth::authenticateOrThrow(...AdminPanel::$authStructures);


                    if (Auth::subAccountAuth()) {

                        $structure = SubAccountsSchema::baseStructure();
                    } else {

                        $structure = null;


                        foreach (AdminPanel::$authStructures as $user) {

                            if ($user->collection == Auth::getAuthCollection()) {
                                $structure = $user;
                                break;
                            }
                        }
                    }
                    if (!$structure) {
                        Response::validation(I18n::get('access_denied'));
                    }


                    $passwordField =  $structure->findItemByType(Shm::password())?->key;

                    if (!$passwordField) {
                        Response::validation(\Shm\ShmUtils\I18n::get('cant_change_password'));
                    }

                    $currentPassword = $args['currentPassword'];
                    $newPassword = $args['newPassword'];

                    //Проверка что текущий пароль правильный


                    $passwordHash = Auth::getAuthOwnerField($passwordField, null);
                    $newPasswordHash = Auth::getPassword($newPassword);

                    if ($passwordHash) {
                        if ($passwordHash != Auth::getPassword($currentPassword)) {

                            Response::validation(\Shm\ShmUtils\I18n::get('invalid_current_password'));
                        }



                        if ($newPasswordHash == $passwordHash) {
                            Response::validation(\Shm\ShmUtils\I18n::get('new_password_cant_be_the_same_as_current'));
                        }
                    }

                    $structure->updateOne(
                        [
                            "_id" => Auth::subAccountAuth() ? Auth::getSubAccountID() : Auth::getAuthID()
                        ],
                        [
                            '$set' => [
                                $passwordField => $newPasswordHash
                            ]
                        ]
                    );

                    return true;
                }
            ];
        });
    }
    /*  public static function rpc()
    {

        return ShmRPC::lazy(function () {

            return [
                'type' => Shm::structure([
                    "_id" => Shm::ID(),
                    "*" => Shm::mixed(),
                ]),


                'args' => Shm::structure([

                    'values' => Shm::mixed(),

                ]),
                'resolve' => function ($root, $args) {



                    Auth::authenticateOrThrow(...AdminPanel::$authStructures);


                    if (Auth::subAccountAuth()) {

                        $structure = SubAccountsSchema::baseStructure();
                    } else {

                        $structure = null;


                        foreach (AdminPanel::$authStructures as $user) {

                            if ($user->collection == Auth::getAuthCollection()) {
                                $structure = $user;
                                break;
                            }
                        }
                    }
                    if (!$structure) {
                        Response::validation(I18n::get('access_denied'));
                    }



                    $root->setType($structure);



                    $values = $args['values'] ?? null;

                    if (!$values) {
                        Response::validation(I18n::get('no_data_to_update'));
                    }

                    $values = $structure->normalize($values);
                    $values = $structure->removeOtherItems($values);

                    //remove _id
                    if (isset($values['_id'])) {
                        unset($values['_id']);
                    }


                    $eventPayload = new ShmEventPayload();
                    $eventPayload->preUpdatePayload = new ShmEventPreUpdatePayload();
                    $eventPayload->preUpdatePayload->filter = [
                        "_id" => Auth::subAccountAuth() ? Auth::getSubAccountID() : Auth::getAuthID()
                    ];
                    $eventPayload->preUpdatePayload->update = [
                        '$set' => $values
                    ];
                    $structure->callEventPreUpdateAdmin($eventPayload);


                    $structure->updateOne(
                        [
                            "_id" => Auth::subAccountAuth() ? Auth::getSubAccountID() : Auth::getAuthID()
                        ],
                        [
                            '$set' => $values
                        ]
                    );

                    $eventPayload->postUpdatePayload = new ShmEventPostUpdatePayload();
                    $eventPayload->postUpdatePayload->filter = [
                        "_id" => Auth::subAccountAuth() ? Auth::getSubAccountID() : Auth::getAuthID()
                    ];
                    $eventPayload->postUpdatePayload->update = [
                        '$set' => $values
                    ];
                    $structure->callEventPostUpdateAdmin($eventPayload);

                    return  $structure->findOne([
                        '_id' => Auth::subAccountAuth() ? Auth::getSubAccountID() : Auth::getAuthID()
                    ]);
                }

            ];
        });
    }*/
}