<?php

namespace Shm\ShmAdmin\AdminRPC;

use Shm\Shm;
use Shm\ShmAdmin\AdminPanel;
use Shm\ShmAdmin\SchemaCollections\SubAccounts;
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 = SubAccounts::structure();
                    } 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;
                }
            ];
        });
    }
}
