<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

/**
 * 微信支付服务类
 */
final class WechatPaymentService
{
    /**
     * 构造函数
     */
    public function __construct(
        private readonly Container $app
    ) {}

    /**
     * 统一下单
     */
    public function unifiedOrder(array $params, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->order->unify($params);

        return [
            'code' => 'SUCCESS',
            'message' => '统一下单成功',
            'data' => $result
        ];
    }

    /**
     * 订单查询
     */
    public function queryOrder(string $outTradeNo, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->order->queryByOutTradeNumber($outTradeNo);

        return [
            'code' => 'SUCCESS',
            'message' => '订单查询成功',
            'data' => $result
        ];
    }

    /**
     * 关闭订单
     */
    public function closeOrder(string $outTradeNo, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->order->close($outTradeNo);

        return [
            'code' => 'SUCCESS',
            'message' => '关闭订单成功',
            'data' => $result
        ];
    }

    /**
     * 申请退款
     */
    public function refund(array $params, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->refund->create($params);

        return [
            'code' => 'SUCCESS',
            'message' => '申请退款成功',
            'data' => $result
        ];
    }

    /**
     * 查询退款
     */
    public function queryRefund(string $refundNo, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->refund->queryByRefundNumber($refundNo);

        return [
            'code' => 'SUCCESS',
            'message' => '查询退款成功',
            'data' => $result
        ];
    }

    /**
     * 企业付款
     */
    public function transfer(array $params, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->transfer->toBalance($params);

        return [
            'code' => 'SUCCESS',
            'message' => '企业付款成功',
            'data' => $result
        ];
    }

    /**
     * 扫码支付
     */
    public function scanPay(array $params, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->order->unify($params);

        return [
            'code' => 'SUCCESS',
            'message' => '扫码支付成功',
            'data' => $result
        ];
    }

    /**
     * H5支付
     */
    public function h5Pay(array $params, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->order->unify($params);

        return [
            'code' => 'SUCCESS',
            'message' => 'H5支付成功',
            'data' => $result
        ];
    }

    /**
     * 小程序支付
     */
    public function miniProgramPay(array $params, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->order->unify($params);

        return [
            'code' => 'SUCCESS',
            'message' => '小程序支付成功',
            'data' => $result
        ];
    }

    /**
     * 公众号支付
     */
    public function officialAccountPay(array $params, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->order->unify($params);

        return [
            'code' => 'SUCCESS',
            'message' => '公众号支付成功',
            'data' => $result
        ];
    }

    /**
     *  APP支付
     */
    public function appPay(array $params, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->order->unify($params);

        return [
            'code' => 'SUCCESS',
            'message' => 'APP支付成功',
            'data' => $result
        ];
    }

    /**
     * 生成支付参数
     */
    public function generatePayParams(array $prepayId, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->jssdk->bridgeConfig($prepayId['prepay_id']);

        return [
            'code' => 'SUCCESS',
            'message' => '生成支付参数成功',
            'data' => $result
        ];
    }

    /**
     * 验证回调通知
     */
    public function verifyNotify(callable $callback, string $driver = 'payment', string $name = 'default'): void
    {
        $app = $this->getApp($driver, $name);
        
        $app->handlePaidNotify($callback);
    }

    /**
     * 获取微信应用实例
     */
    protected function getApp(string $driver, string $name): mixed
    {
        return $this->app->make('kylin.wechat')->driver($driver, $name);
    }
}
