<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

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

    /**
     * 创建菜单
     */
    public function create(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 get(string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->menu->get();

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

    /**
     * 删除菜单
     */
    public function delete(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 current(string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->menu->current();

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

    /**
     * 创建个性化菜单
     */
    public function createConditional(array $menu, array $matchRule, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->menu->createConditional($menu, $matchRule);

        return [
            'code' => 'SUCCESS',
            'message' => '创建个性化菜单成功',
            'data' => $result
        ];
    }

    /**
     * 删除个性化菜单
     */
    public function deleteConditional(string $menuId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->menu->deleteConditional($menuId);

        return [
            'code' => 'SUCCESS',
            'message' => '删除个性化菜单成功',
            'data' => $result
        ];
    }

    /**
     * 测试个性化菜单
     */
    public function testConditional(string $openId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->menu->testConditional($openId);

        return [
            'code' => 'SUCCESS',
            'message' => '测试个性化菜单成功',
            'data' => $result
        ];
    }

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