<?php

namespace Shm\ShmPayment;

use Shm\ShmDB\mDB;
use Shm\ShmHttp\ShmHttp;
use Shm\ShmTypes\CompositeTypes\BalanceTypes\BalanceGateway;
use Shm\ShmTypes\StructureType;
use Shm\ShmUtils\ShmInit;

class ShmAppTorPayment
{
    public static function yoomoneyGateway($functionStructure): BalanceGateway
    {


        $gateway = new BalanceGateway('yoomoney');
        $gateway->setTitle('Картой онлайн');
        $gateway->setDescription('Оплата картой онлайн');
        $gateway->setMinAmount(1000);
        $gateway->setMaxAmount(15000);
        $gateway->setPaymentLinkGenerator(function ($user_id, $amount) use ($functionStructure): string {



            $structure = $functionStructure();

            $result = ShmHttp::post("https://payment.wgt.click/api/payment", [
                'method' => "yoomoney",
                'params' => [
                    "amount" => $amount,
                    'currency' => "RUB",
                    'webhook' => self::url('/appTorPayment/webhook'),
                    'payload' => [
                        "owner" => $user_id,
                        'ownerCollection' => $structure->collection,
                    ],
                ]
            ]);


            mDB::collection("_appTorPayment")->insertOne($result['result']);


            return $result['result']['link'];
        });


        return $gateway;
    }

    public static function invoiceGateway($functionStructure): BalanceGateway
    {
        $gateway = new BalanceGateway('invoice');
        $gateway->setTitle('Оплат по счету');
        $gateway->setDescription('Оплата по счету');

        $gateway->setPaymentLinkGenerator(function ($user_id, $amount) use ($functionStructure): string {


            $structure = $functionStructure();

            $result = ShmHttp::post("https://payment.wgt.click/api/payment", [
                'method' => "invoice",
                'params' => [
                    "amount" => $amount,
                    'info' => "Пополнение баланса личного кабинета №" . $user_id . " по договору-оферте. Без НДС.",
                    'currency' => "RUB",
                    'payload' => [
                        "owner" =>  mDB::id($user_id),
                        'ownerCollection' => $structure->collection,
                    ],
                ]
            ]);


            mDB::collection("_appTorPayment")->insertOne($result['result']);


            return $result['result']['link'];
        });
        return $gateway;
    }

    private static function url($path = "")
    {


        $scheme =  (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';

        $host = $_SERVER['HTTP_HOST'] ?? "";

        $currentURL = $scheme . '://' . $host;

        return $currentURL . $path;
    }

    public static function webhookInit()
    {

        $f3 = \Base::instance();
        $f3->route('POST|GET /appTorPayment/webhook', function ($f3) {


            var_dump(self::url('/appTorPayment/webhook'));
            exit;
        });
    }
}
