<?php

namespace Kylin\Payment\Scenarios;

use Kylin\Payment\Payment;

/**
 * 订阅支付场景类
 */
class SubscriptionPayment
{
    /**
     * 构造函数
     */
    public function __construct(
        protected readonly string $paymentType,
        protected readonly string $name = 'default'
    ) {}

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

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

    /**
     * 取消订阅
     */
    public function cancel(string $subscriptionId): mixed
    {
        return Payment::close($this->paymentType, $subscriptionId, $this->name);
    }

    /**
     * 暂停订阅
     */
    public function pause(string $subscriptionId): mixed
    {
        return Payment::query($this->paymentType, $subscriptionId, $this->name);
    }

    /**
     * 恢复订阅
     */
    public function resume(string $subscriptionId): mixed
    {
        return Payment::query($this->paymentType, $subscriptionId, $this->name);
    }

    /**
     * 查询订阅状态
     */
    public function status(string $subscriptionId): mixed
    {
        return Payment::query($this->paymentType, $subscriptionId, $this->name);
    }

    /**
     * 更新订阅信息
     */
    public function update(string $subscriptionId, array $params): mixed
    {
        return Payment::pay($this->paymentType, array_merge($params, ['subscription_id' => $subscriptionId]), $this->name);
    }

    /**
     * 获取支持的订阅周期
     */
    public function getSupportedPeriods(): array
    {
        // 根据支付类型返回支持的订阅周期
        $periodsMap = [
            'paypal' => ['day', 'week', 'month', 'year'],
            'stripe' => ['day', 'week', 'month', 'year'],
            'alipay' => ['month', 'year'],
            'wechat' => ['month', 'year'],
        ];

        return $periodsMap[$this->paymentType] ?? [];
    }
}
