<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

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

    /**
     * 获取用户信息
     */
    public function getUserInfo(string $openId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->user->get($openId);

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

    /**
     * 获取用户列表
     */
    public function getUserList(int $nextOpenId = 0, 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 batchGetUserInfo(array $openIds, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->user->batchGet($openIds);

        return [
            'code' => 'SUCCESS',
            'message' => '批量获取用户信息成功',
            'data' => $result
        ];
    }

    /**
     * 更新用户备注
     */
    public function updateRemark(string $openId, string $remark, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->user->remark($openId, $remark);

        return [
            'code' => 'SUCCESS',
            'message' => '更新用户备注成功',
            'data' => $result
        ];
    }

    /**
     * 创建标签
     */
    public function createTag(string $tagName, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->user_tag->create($tagName);

        return [
            'code' => 'SUCCESS',
            'message' => '创建标签成功',
            'data' => $result
        ];
    }

    /**
     * 获取标签列表
     */
    public function getTagList(string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->user_tag->list();

        return [
            'code' => 'SUCCESS',
            'message' => '获取标签列表成功',
            'data' => $result
        ];
    }

    /**
     * 编辑标签
     */
    public function updateTag(int $tagId, string $tagName, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->user_tag->update($tagId, $tagName);

        return [
            'code' => 'SUCCESS',
            'message' => '编辑标签成功',
            'data' => $result
        ];
    }

    /**
     * 删除标签
     */
    public function deleteTag(int $tagId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->user_tag->delete($tagId);

        return [
            'code' => 'SUCCESS',
            'message' => '删除标签成功',
            'data' => $result
        ];
    }

    /**
     * 给用户打标签
     */
    public function tagUsers(array $openIds, int $tagId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->user_tag->tagUsers($openIds, $tagId);

        return [
            'code' => 'SUCCESS',
            'message' => '给用户打标签成功',
            'data' => $result
        ];
    }

    /**
     * 取消用户标签
     */
    public function untagUsers(array $openIds, int $tagId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->user_tag->untagUsers($openIds, $tagId);

        return [
            'code' => 'SUCCESS',
            'message' => '取消用户标签成功',
            'data' => $result
        ];
    }

    /**
     * 获取用户标签列表
     */
    public function getUserTags(string $openId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->user_tag->userTags($openId);

        return [
            'code' => 'SUCCESS',
            'message' => '获取用户标签列表成功',
            'data' => $result
        ];
    }

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