<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

/**
 * 微信二维码服务类
 */
final class WechatQrCodeService
{
    /**
     * 构造函数
     */
    public function __construct(
        private readonly Container $app
    ) {}

    /**
     * 创建临时二维码
     */
    public function createTemporary(int $sceneId, int $expireSeconds = 604800, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->qrcode->temporary($sceneId, $expireSeconds);

        return [
            'code' => 'SUCCESS',
            'message' => '创建临时二维码成功',
            'data' => $result
        ];
    }

    /**
     * 创建永久二维码
     */
    public function createPermanent(int $sceneId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->qrcode->forever($sceneId);

        return [
            'code' => 'SUCCESS',
            'message' => '创建永久二维码成功',
            'data' => $result
        ];
    }

    /**
     * 创建永久字符串二维码
     */
    public function createPermanentString(string $sceneStr, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->qrcode->forever($sceneStr);

        return [
            'code' => 'SUCCESS',
            'message' => '创建永久字符串二维码成功',
            'data' => $result
        ];
    }

    /**
     * 获取二维码 URL
     */
    public function getUrl(string $ticket, string $driver = 'official_account', string $name = 'default'): string
    {
        $app = $this->getApp($driver, $name);
        
        return $app->qrcode->url($ticket);
    }

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