<?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\TNominalBankDetails;
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 AdminRPCTCompanyBankUpdate
{
    public static function rpc()
    {

        return ShmRPC::lazy(function () {

            return [
                'type' => TNominalBankDetails::structure(),
                'args' => Shm::structure([
                    ...TNominalBankDetails::editableFields(),
                ]),
                'resolve' => function ($root, $args) {



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




                    $company = TNominalBankDetails::structure()->findOne([
                        'owner' => Auth::getAuthID(),
                        'ownerCollection' =>  Auth::getAuthCollection(),
                    ]);
                    if (!$company) {
                        TNominalBankDetails::structure()->insertOne([
                            'owner' => Auth::getAuthID(),
                            'ownerCollection' =>  Auth::getAuthCollection(),
                            ...$args,
                        ]);
                    } else {

                        //Редактируем если статус черновик
                        if ($company['status'] != 'draft') {
                            Shm::error("Вы не можете редактировать данные банка, так как они уже были отправлены на проверку");
                        }

                        TNominalBankDetails::structure()->updateOne([
                            '_id' => $company['_id'],
                        ], [
                            '$set' => $args,
                        ]);
                    }


                    return TNominalBankDetails::structure()->find([
                        'owner' => Auth::getAuthID(),
                        'ownerCollection' =>  Auth::getAuthCollection(),
                    ]);
                }

            ];
        });
    }
}