<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

/**
 * 微信订阅号服务类
 */
final class WechatSubscriptionService
{
    /**
     * 构造函数
     */
    public function __construct(
        private readonly Container $app
    ) {}

    /**
     * 获取订阅号信息
     */
    public function getAccountInfo(string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->account->info();

        return [
            'code' => 'SUCCESS',
            'message' => '获取订阅号信息成功',
            'data' => $result
        ];
    }

    /**
     * 修改订阅号头像
     */
    public function updateAvatar(string $imagePath, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->account->updateAvatar($imagePath);

        return [
            'code' => 'SUCCESS',
            'message' => '修改订阅号头像成功',
            'data' => $result
        ];
    }

    /**
     * 修改订阅号名称
     */
    public function updateName(string $accountName, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->account->updateName($accountName);

        return [
            'code' => 'SUCCESS',
            'message' => '修改订阅号名称成功',
            'data' => $result
        ];
    }

    /**
     * 修改订阅号简介
     */
    public function updateDescription(string $description, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->account->updateDescription($description);

        return [
            'code' => 'SUCCESS',
            'message' => '修改订阅号简介成功',
            'data' => $result
        ];
    }

    /**
     * 获取订阅号菜单
     */
    public function getMenu(string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->menu->all();

        return [
            'code' => 'SUCCESS',
            'message' => '获取订阅号菜单成功',
            'data' => $result
        ];
    }

    /**
     * 创建订阅号菜单
     */
    public function createMenu(array $menu, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->menu->create($menu);

        return [
            'code' => 'SUCCESS',
            'message' => '创建订阅号菜单成功',
            'data' => $result
        ];
    }

    /**
     * 删除订阅号菜单
     */
    public function deleteMenu(string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->menu->delete();

        return [
            'code' => 'SUCCESS',
            'message' => '删除订阅号菜单成功',
            'data' => $result
        ];
    }

    /**
     * 获取订阅号粉丝列表
     */
    public function getFansList(string $nextOpenid = '', string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->user->list($nextOpenid);

        return [
            'code' => 'SUCCESS',
            'message' => '获取订阅号粉丝列表成功',
            'data' => $result
        ];
    }

    /**
     * 获取订阅号粉丝信息
     */
    public function getFanInfo(string $openid, string $lang = 'zh_CN', string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->user->get($openid, $lang);

        return [
            'code' => 'SUCCESS',
            'message' => '获取订阅号粉丝信息成功',
            'data' => $result
        ];
    }

    /**
     * 批量获取订阅号粉丝信息
     */
    public function getFanInfoBatch(array $openids, string $lang = 'zh_CN', string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->user->batchGet($openids, $lang);

        return [
            'code' => 'SUCCESS',
            'message' => '批量获取订阅号粉丝信息成功',
            'data' => $result
        ];
    }

    /**
     * 发送订阅号模板消息
     */
    public function sendTemplateMessage(array $message, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->template_message->send($message);

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

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