<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

/**
 * 微信安全风控服务类
 */
final class WechatSecurityService
{
    /**
     * 构造函数
     */
    public function __construct(
        private readonly Container $app
    ) {}

    /**
     * 消息加密
     */
    public function encryptMessage(string $message, string $appId, string $token, string $encodingAesKey): array
    {
        $app = $this->getApp('official_account');
        
        $result = $app->server->encryptMessage($message, $appId, $token, $encodingAesKey);

        return [
            'code' => 'SUCCESS',
            'message' => '消息加密成功',
            'data' => $result
        ];
    }

    /**
     * 消息解密
     */
    public function decryptMessage(string $message, string $msgSignature, string $timestamp, string $nonce, string $token, string $encodingAesKey): array
    {
        $app = $this->getApp('official_account');
        
        $result = $app->server->decryptMessage($message, $msgSignature, $timestamp, $nonce, $token, $encodingAesKey);

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

    /**
     * 内容安全检测
     */
    public function checkContent(string $content, string $type = 'text', string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->security->checkContent($content, $type);

        return [
            'code' => 'SUCCESS',
            'message' => '内容安全检测成功',
            'data' => $result
        ];
    }

    /**
     * 图片安全检测
     */
    public function checkImage(string $imagePath, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->security->checkImage($imagePath);

        return [
            'code' => 'SUCCESS',
            'message' => '图片安全检测成功',
            'data' => $result
        ];
    }

    /**
     * 音频安全检测
     */
    public function checkVoice(string $voicePath, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->security->checkVoice($voicePath);

        return [
            'code' => 'SUCCESS',
            'message' => '音频安全检测成功',
            'data' => $result
        ];
    }

    /**
     * 视频安全检测
     */
    public function checkVideo(string $videoPath, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->security->checkVideo($videoPath);

        return [
            'code' => 'SUCCESS',
            'message' => '视频安全检测成功',
            'data' => $result
        ];
    }

    /**
     * 媒体安全检测
     */
    public function checkMedia(string $mediaId, string $type, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->security->checkMedia($mediaId, $type);

        return [
            'code' => 'SUCCESS',
            'message' => '媒体安全检测成功',
            'data' => $result
        ];
    }

    /**
     * 网络链接安全检测
     */
    public function checkUrl(string $url, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->security->checkUrl($url);

        return [
            'code' => 'SUCCESS',
            'message' => '网络链接安全检测成功',
            'data' => $result
        ];
    }

    /**
     * 小程序码安全检测
     */
    public function checkMiniProgramCode(string $codeUrl, string $driver = 'miniprogram', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->security->checkMiniProgramCode($codeUrl);

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

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