<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

/**
 * 微信人工智能服务类
 */
final class WechatAiService
{
    /**
     * 构造函数
     */
    public function __construct(
        private readonly Container $app
    ) {}

    /**
     * 智能对话
     */
    public function chat(string $content, string $userId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->ai->chat($content, $userId);

        return [
            'code' => 'SUCCESS',
            'message' => '智能对话成功',
            'data' => $result
        ];
    }

    /**
     * 文本识别
     */
    public function recognizeText(string $imagePath, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->ai->ocr($imagePath);

        return [
            'code' => 'SUCCESS',
            'message' => '文本识别成功',
            'data' => $result
        ];
    }

    /**
     * 语音识别
     */
    public function recognizeVoice(string $voicePath, string $format = 'amr', int $rate = 8000, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->ai->asr($voicePath, $format, $rate);

        return [
            'code' => 'SUCCESS',
            'message' => '语音识别成功',
            'data' => $result
        ];
    }

    /**
     * 语音合成
     */
    public function synthesizeVoice(string $text, string $lang = 'zh_CN', int $speed = 100, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->ai->tts($text, $lang, $speed);

        return [
            'code' => 'SUCCESS',
            'message' => '语音合成成功',
            'data' => $result
        ];
    }

    /**
     * 图像处理
     */
    public function processImage(string $imagePath, array $options, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->ai->imageProcess($imagePath, $options);

        return [
            'code' => 'SUCCESS',
            'message' => '图像处理成功',
            'data' => $result
        ];
    }

    /**
     * 图像识别
     */
    public function recognizeImage(string $imagePath, string $type = 'all', string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->ai->imageRecognition($imagePath, $type);

        return [
            'code' => 'SUCCESS',
            'message' => '图像识别成功',
            'data' => $result
        ];
    }

    /**
     * 人脸识别
     */
    public function recognizeFace(string $imagePath, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->ai->faceRecognition($imagePath);

        return [
            'code' => 'SUCCESS',
            'message' => '人脸识别成功',
            'data' => $result
        ];
    }

    /**
     * 翻译
     */
    public function translate(string $text, string $from = 'auto', string $to = 'zh_CN', string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->ai->translate($text, $from, $to);

        return [
            'code' => 'SUCCESS',
            'message' => '翻译成功',
            'data' => $result
        ];
    }

    /**
     * 情感分析
     */
    public function sentimentAnalysis(string $text, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->ai->sentiment($text);

        return [
            'code' => 'SUCCESS',
            'message' => '情感分析成功',
            'data' => $result
        ];
    }

    /**
     * 关键词提取
     */
    public function extractKeywords(string $text, int $count = 10, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->ai->keywords($text, $count);

        return [
            'code' => 'SUCCESS',
            'message' => '关键词提取成功',
            'data' => $result
        ];
    }

    /**
     * 文本摘要
     */
    public function summarizeText(string $text, int $maxLength = 100, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->ai->summary($text, $maxLength);

        return [
            'code' => 'SUCCESS',
            'message' => '文本摘要成功',
            'data' => $result
        ];
    }

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