<?php

declare(strict_types=1);

namespace Kylin\Pay\Plugin\Wechat\V3\Marketing\Fapiao\Blockchain;

use Closure;
use Kylin\Pay\Artful\Contract\PluginInterface;
use Kylin\Pay\Artful\Exception\ContainerException;
use Kylin\Pay\Artful\Exception\InvalidConfigException;
use Kylin\Pay\Artful\Exception\InvalidParamsException;
use Kylin\Pay\Artful\Exception\ServiceNotFoundException;
use Kylin\Pay\Artful\Logger;
use Kylin\Pay\Artful\Rocket;
use Kylin\Pay\Exception\DecryptException;
use Kylin\Pay\Supports\Collection;

use function Kylin\Pay\encrypt_wechat_contents;
use function Kylin\Pay\get_provider_config;
use function Kylin\Pay\get_wechat_public_key;
use function Kylin\Pay\get_wechat_serial_no;

/**
 * @see https://pay.weixin.qq.com/docs/merchant/apis/fapiao/fapiao-applications/issue-fapiao-applications.html
 */
class CreatePlugin implements PluginInterface
{
    /**
     * @throws ContainerException
     * @throws DecryptException
     * @throws InvalidConfigException
     * @throws InvalidParamsException
     * @throws ServiceNotFoundException
     */
    public function assembly(Rocket $rocket, Closure $next): Rocket
    {
        Logger::debug('[Wechat][V3][Marketing][Fapiao][Blockchain][CreatePlugin] 插件开始装载', ['rocket' => $rocket]);

        $params = $rocket->getParams();
        $payload = $rocket->getPayload();
        $config = get_provider_config('wechat', $params);

        $rocket->mergePayload(array_merge([
            '_method' => 'POST',
            '_url' => 'v3/new-tax-control-fapiao/fapiao-applications',
        ], $this->encryptSensitiveData($payload, $params, $config)));

        Logger::info('[Wechat][V3][Marketing][Fapiao][Blockchain][CreatePlugin] 插件装载完毕', ['rocket' => $rocket]);

        return $next($rocket);
    }

    /**
     * @throws ContainerException
     * @throws InvalidConfigException
     * @throws InvalidParamsException
     * @throws ServiceNotFoundException
     * @throws DecryptException
     */
    protected function encryptSensitiveData(?Collection $payload, array $params, array $config): array
    {
        $data['_serial_no'] = get_wechat_serial_no($params);

        $config = get_provider_config('wechat', $params);
        $publicKey = get_wechat_public_key($config, $data['_serial_no']);

        $phone = $payload?->get('buyer_information.phone') ?? null;
        $email = $payload?->get('buyer_information.email') ?? null;

        if (!is_null($phone)) {
            $data['buyer_information']['phone'] = encrypt_wechat_contents($phone, $publicKey);
        }

        if (!is_null($email)) {
            $data['buyer_information']['email'] = encrypt_wechat_contents($email, $publicKey);
        }

        return $data;
    }
}
