<?php

namespace Shm\ShmAdmin\AdminRPC;

use Shm\Shm;
use Shm\ShmAdmin\AdminPanel;
use Shm\ShmAuth\Auth;
use Shm\ShmDB\mDB;
use Shm\ShmIntegrations\TBankNominal\TBankCollections\TNominalCompanies;
use Shm\ShmRPC\ShmRPC;
use Shm\ShmSocial\ShmSocials;
use Shm\ShmUtils\I18n;
use Shm\ShmUtils\Response;
use Shm\ShmUtils\ShmManticore;

class AdminRPCTCompanySend
{
    public static function rpc()
    {

        return ShmRPC::lazy(function () {

            return [
                'type' => TNominalCompanies::structure(),

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



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




                    $company = TNominalCompanies::structure()->findOne([
                        'owner' => Auth::getAuthID(),
                        'ownerCollection' =>  Auth::getAuthCollection(),
                    ]);
                    if (!$company) {
                        ShmRPC::error(I18n::get('company_not_found'));
                    }

                    TNominalCompanies::validateCompany($company);


                    TNominalCompanies::structure()->updateOne([
                        '_id' => $company['_id'],
                    ], [
                        '$set' => [
                            'status' => 'pending',
                        ],
                    ]);

                    return TNominalCompanies::structure()->findOne([
                        '_id' => $company['_id'],
                    ]);
                }

            ];
        });
    }
}