<?php

namespace Shm\ShmIntegrations\OneSignal;

use Shm\Shm;
use Shm\ShmAuth\Auth;
use Shm\ShmDB\mDB;
use Shm\ShmRPC\ShmRPC;

class OneSignalPush
{


    private $onesignalKeys = [];


    public static function addKey($appId, $appKey)
    {
        self::$onesignalKeys[$appId] = $appKey;
    }

    public static function rpc()
    {

        return [


            "onePushSubscription"  => ShmRPC::lazy(function () {
                return [
                    'type' => Shm::bool(),
                    'args' => [
                        'appId' => Shm::nonNull(Shm::string()),
                        'playerId' => Shm::nonNull(Shm::string()),
                    ],
                    'resolve' => function ($root, $args) {



                        mDB::collection("_onePushSubscriptions")->updateOne([
                            'playerId' => $args['playerId'],
                        ], [
                            '$set' => [
                                'owner' => Auth::getAuthID(),
                                'ownerCollection' => Auth::getAuthCollection(),
                                'appId' => $args['appId'],
                                'playerId' => $args['playerId'],
                            ],
                        ], [
                            'upsert' => true,
                        ]);

                        return true;
                    },
                ];
            }),


        ];
    }
}
