<?php

namespace Kylin\Payment\Scenarios;

use Kylin\Payment\Payment;

/**
 * 积分兑换支付场景类
 */
class PointsExchangePayment
{
    /**
     * 构造函数
     */
    public function __construct(
        protected readonly string $paymentType,
        protected readonly string $name = 'default'
    ) {}

    /**
     * 创建积分兑换订单
     */
    public function create(array $params): mixed
    {
        // 确保参数中包含积分兑换相关信息
        $params['points'] = $params['points'] ?? [];

        // 调用支付方法
        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 exchange(string $productId, array $params): mixed
    {
        return Payment::pay($this->paymentType, array_merge($params, ['product_id' => $productId, 'exchange' => true]), $this->name);
    }

    /**
     * 查看积分余额
     */
    public function balance(string $userId): mixed
    {
        return Payment::query($this->paymentType, $userId, $this->name);
    }

    /**
     * 积分充值
     */
    public function recharge(string $userId, array $params): mixed
    {
        return Payment::pay($this->paymentType, array_merge($params, ['user_id' => $userId, 'recharge' => true]), $this->name);
    }

    /**
     * 积分提现
     */
    public function withdraw(string $userId, array $params): mixed
    {
        return Payment::pay($this->paymentType, array_merge($params, ['user_id' => $userId, 'withdraw' => true]), $this->name);
    }
}
