<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

/**
 * 微信小程序服务类
 */
final class WechatMiniProgramService
{
    /**
     * 构造函数
     */
    public function __construct(
        private readonly Container $app
    ) {}

    /**
     * 生成小程序码
     */
    public function createQrCode(string $path, int $width = 430, bool $autoColor = false, array $lineColor = [], bool $isHyaline = false, string $driver = 'mini_program', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->app_code->get($path, [
            'width' => $width,
            'auto_color' => $autoColor,
            'line_color' => $lineColor,
            'is_hyaline' => $isHyaline,
        ]);

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

    /**
     * 生成带参数的小程序码
     */
    public function createQrCodeWithScene(string $scene, string $page = '', int $width = 430, bool $autoColor = false, array $lineColor = [], bool $isHyaline = false, string $driver = 'mini_program', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->app_code->getUnlimit($scene, [
            'page' => $page,
            'width' => $width,
            'auto_color' => $autoColor,
            'line_color' => $lineColor,
            'is_hyaline' => $isHyaline,
        ]);

        return [
            'code' => 'SUCCESS',
            'message' => '生成带参数的小程序码成功',
            'data' => $result
        ];
    }

    /**
     * 发送模板消息
     */
    public function sendTemplateMessage(string $openId, string $templateId, string $page, array $data, array $miniprogram = [], string $driver = 'mini_program', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->template_message->send([
            'touser' => $openId,
            'template_id' => $templateId,
            'page' => $page,
            'form_id' => $data['form_id'] ?? '',
            'data' => $data,
            'emphasis_keyword' => $data['emphasis_keyword'] ?? '',
        ]);

        return [
            'code' => 'SUCCESS',
            'message' => '发送模板消息成功',
            'data' => $result
        ];
    }

    /**
     * 发送订阅消息
     */
    public function sendSubscriptionMessage(string $openId, string $templateId, array $data, string $page = '', array $miniprogram = [], string $driver = 'mini_program', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->subscribe_message->send([
            'touser' => $openId,
            'template_id' => $templateId,
            'page' => $page,
            'data' => $data,
            'miniprogram' => $miniprogram,
        ]);

        return [
            'code' => 'SUCCESS',
            'message' => '发送订阅消息成功',
            'data' => $result
        ];
    }

    /**
     * 获取小程序码
     */
    public function getQrCode(string $path, int $width = 430, string $driver = 'mini_program', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->code->get($path, $width);

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

    /**
     * 获取小程序页面列表
     */
    public function getPageList(string $driver = 'mini_program', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->code->getPage();

        return [
            'code' => 'SUCCESS',
            'message' => '获取小程序页面列表成功',
            'data' => $result
        ];
    }

    /**
     * 解密用户信息
     */
    public function decryptData(string $sessionKey, string $encryptedData, string $iv, string $driver = 'mini_program', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->encryptor->decryptData($sessionKey, $encryptedData, $iv);

        return [
            'code' => 'SUCCESS',
            'message' => '解密用户信息成功',
            'data' => $result
        ];
    }

    /**
     * 获取小程序码
     */
    public function createWxaQrCode(string $path, int $width = 430, string $driver = 'mini_program', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->code->create($path, $width);

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

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