<?php

namespace Kylin\Payment\Scenarios;

use Kylin\Payment\Payment;

/**
 * 虚拟商品支付场景类
 */
class VirtualGoodsPayment
{
    /**
     * 构造函数
     */
    public function __construct(
        protected readonly string $paymentType,
        protected readonly string $name = 'default'
    ) {}

    /**
     * 创建虚拟商品订单
     */
    public function create(array $params): mixed
    {
        // 确保参数中包含虚拟商品相关信息
        $params['virtual'] = $params['virtual'] ?? [];

        // 调用支付方法
        return Payment::pay($this->paymentType, $params, $this->name);
    }

    /**
     * 支付虚拟商品订单
     */
    public function pay(string $orderId, array $params): mixed
    {
        return Payment::pay($this->paymentType, array_merge($params, ['order_id' => $orderId]), $this->name);
    }

    /**
     * 取消虚拟商品订单
     */
    public function cancel(string $orderId): mixed
    {
        return Payment::close($this->paymentType, $orderId, $this->name);
    }

    /**
     * 查询虚拟商品订单状态
     */
    public function status(string $orderId): mixed
    {
        return Payment::query($this->paymentType, $orderId, $this->name);
    }

    /**
     * 发放虚拟商品
     */
    public function deliver(string $orderId): mixed
    {
        return Payment::query($this->paymentType, $orderId, $this->name);
    }

    /**
     * 验证虚拟商品
     */
    public function verify(string $productId, string $token): mixed
    {
        return Payment::query($this->paymentType, $productId, $this->name);
    }

    /**
     * 退款虚拟商品
     */
    public function refund(string $orderId, array $params): mixed
    {
        return Payment::refund($this->paymentType, array_merge($params, ['order_id' => $orderId]), $this->name);
    }

    /**
     * 批量购买虚拟商品
     */
    public function batch(array $params): mixed
    {
        // 确保参数中包含批量购买相关信息
        $params['batch'] = $params['batch'] ?? [];

        // 调用支付方法
        return Payment::pay($this->paymentType, $params, $this->name);
    }
}
