<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;
use EasyWeChat\OfficialAccount\Application as OfficialAccountApplication;
use EasyWeChat\MiniProgram\Application as MiniProgramApplication;

/**
 * 微信消息服务类
 */
final class WechatMessageService
{
    /**
     * 构造函数
     */
    public function __construct(
        private readonly Container $app
    ) {}

    /**
     * 发送文本消息
     */
    public function sendText(string $openId, string $content, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->message->send([
            'touser' => $openId,
            'msgtype' => 'text',
            'text' => [
                'content' => $content
            ]
        ]);

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

    /**
     * 发送图片消息
     */
    public function sendImage(string $openId, string $mediaId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->message->send([
            'touser' => $openId,
            'msgtype' => 'image',
            'image' => [
                'media_id' => $mediaId
            ]
        ]);

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

    /**
     * 发送语音消息
     */
    public function sendVoice(string $openId, string $mediaId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->message->send([
            'touser' => $openId,
            'msgtype' => 'voice',
            'voice' => [
                'media_id' => $mediaId
            ]
        ]);

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

    /**
     * 发送视频消息
     */
    public function sendVideo(string $openId, string $mediaId, string $title, string $description, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->message->send([
            'touser' => $openId,
            'msgtype' => 'video',
            'video' => [
                'media_id' => $mediaId,
                'title' => $title,
                'description' => $description
            ]
        ]);

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

    /**
     * 发送图文消息
     */
    public function sendNews(string $openId, array $articles, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->message->send([
            'touser' => $openId,
            'msgtype' => 'news',
            'news' => [
                'articles' => $articles
            ]
        ]);

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

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

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

    /**
     * 发送订阅消息
     */
    public function sendSubscription(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 getTemplateList(string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->template_message->list();

        return [
            'code' => 'SUCCESS',
            'message' => '获取消息模板列表成功',
            'data' => $result
        ];
    }

    /**
     * 删除消息模板
     */
    public function deleteTemplate(string $templateId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->template_message->delete($templateId);

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

    /**
     * 获取小程序订阅消息模板列表
     */
    public function getSubscriptionTemplateList(string $driver = 'mini_program', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->subscribe_message->getTemplates();

        return [
            'code' => 'SUCCESS',
            'message' => '获取订阅消息模板列表成功',
            'data' => $result
        ];
    }

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