<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

/**
 * 微信开放平台服务类
 */
final class WechatOpenPlatformService
{
    /**
     * 构造函数
     */
    public function __construct(
        private readonly Container $app
    ) {}

    /**
     * 获取授权链接
     */
    public function getAuthorizationUrl(string $redirectUri, string $scope = 'snsapi_login', string $state = '', string $driver = 'open_platform', string $name = 'default'): string
    {
        $app = $this->getApp($driver, $name);
        
        return $app->oauth->redirect($redirectUri, $scope, $state);
    }

    /**
     * 获取授权码
     */
    public function getAuthorizationCode(string $code, string $driver = 'open_platform', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->oauth->userFromCode($code);

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

    /**
     * 获取第三方平台 component_access_token
     */
    public function getComponentAccessToken(string $driver = 'open_platform', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->component_access_token->getToken();

        return [
            'code' => 'SUCCESS',
            'message' => '获取第三方平台 component_access_token 成功',
            'data' => $result
        ];
    }

    /**
     * 获取预授权码
     */
    public function getPreAuthorizationCode(string $driver = 'open_platform', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->pre_auth_code->getToken();

        return [
            'code' => 'SUCCESS',
            'message' => '获取预授权码成功',
            'data' => $result
        ];
    }

    /**
     * 使用授权码获取授权信息
     */
    public function getAuthorizationInfo(string $authCode, string $driver = 'open_platform', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->authorizer->getAuthorizationInfo($authCode);

        return [
            'code' => 'SUCCESS',
            'message' => '使用授权码获取授权信息成功',
            'data' => $result
        ];
    }

    /**
     * 获取授权方的账号基本信息
     */
    public function getAuthorizerInfo(string $authorizerAppId, string $driver = 'open_platform', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->authorizer->getAuthorizerInfo($authorizerAppId);

        return [
            'code' => 'SUCCESS',
            'message' => '获取授权方的账号基本信息成功',
            'data' => $result
        ];
    }

    /**
     * 获取授权方的选项设置信息
     */
    public function getAuthorizerOption(string $authorizerAppId, string $optionName, string $driver = 'open_platform', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->authorizer->getAuthorizerOption($authorizerAppId, $optionName);

        return [
            'code' => 'SUCCESS',
            'message' => '获取授权方的选项设置信息成功',
            'data' => $result
        ];
    }

    /**
     * 设置授权方的选项设置信息
     */
    public function setAuthorizerOption(string $authorizerAppId, string $optionName, string $optionValue, string $driver = 'open_platform', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->authorizer->setAuthorizerOption($authorizerAppId, $optionName, $optionValue);

        return [
            'code' => 'SUCCESS',
            'message' => '设置授权方的选项设置信息成功',
            'data' => $result
        ];
    }

    /**
     * 删除授权方的选项设置信息
     */
    public function deleteAuthorizerOption(string $authorizerAppId, string $optionName, string $driver = 'open_platform', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->authorizer->deleteAuthorizerOption($authorizerAppId, $optionName);

        return [
            'code' => 'SUCCESS',
            'message' => '删除授权方的选项设置信息成功',
            'data' => $result
        ];
    }

    /**
     * 代开发小程序 - 获取授权小程序的码
     */
    public function createMiniProgramCode(string $authorizerAppId, string $path, int $width = 430, string $driver = 'open_platform', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->mini_program->code->get($authorizerAppId, $path, $width);

        return [
            'code' => 'SUCCESS',
            'message' => '代开发小程序 - 获取授权小程序的码成功',
            'data' => $result
        ];
    }

    /**
     * 代开发小程序 - 发送模板消息
     */
    public function sendMiniProgramTemplateMessage(string $authorizerAppId, array $message, string $driver = 'open_platform', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->mini_program->template_message->send($authorizerAppId, $message);

        return [
            'code' => 'SUCCESS',
            'message' => '代开发小程序 - 发送模板消息成功',
            'data' => $result
        ];
    }

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