<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

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

    /**
     * 获取设备列表
     */
    public function getDeviceList(int $page = 1, int $size = 10, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->device->list($page, $size);

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

    /**
     * 绑定设备
     */
    public function bindDevice(string $deviceId, string $openId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->device->bind($deviceId, $openId);

        return [
            'code' => 'SUCCESS',
            'message' => '绑定设备成功',
            'data' => $result
        ];
    }

    /**
     * 解绑设备
     */
    public function unbindDevice(string $deviceId, string $openId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->device->unbind($deviceId, $openId);

        return [
            'code' => 'SUCCESS',
            'message' => '解绑设备成功',
            'data' => $result
        ];
    }

    /**
     * 绑定设备与门店
     */
    public function bindDeviceToPoi(string $deviceId, string $poiId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->device->bindPoi($deviceId, $poiId);

        return [
            'code' => 'SUCCESS',
            'message' => '绑定设备与门店成功',
            'data' => $result
        ];
    }

    /**
     * 获取设备二维码
     */
    public function getDeviceQrCode(string $deviceId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->device->qrcode($deviceId);

        return [
            'code' => 'SUCCESS',
            'message' => '获取设备二维码成功',
            'data' => $result
        ];
    }

    /**
     * 创建设备组
     */
    public function createDeviceGroup(string $groupName, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->device->groupCreate($groupName);

        return [
            'code' => 'SUCCESS',
            'message' => '创建设备组成功',
            'data' => $result
        ];
    }

    /**
     * 添加设备到设备组
     */
    public function addDeviceToGroup(int $groupId, array $deviceIds, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->device->groupAdd($groupId, $deviceIds);

        return [
            'code' => 'SUCCESS',
            'message' => '添加设备到设备组成功',
            'data' => $result
        ];
    }

    /**
     * 从设备组移除设备
     */
    public function removeDeviceFromGroup(int $groupId, array $deviceIds, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->device->groupRemove($groupId, $deviceIds);

        return [
            'code' => 'SUCCESS',
            'message' => '从设备组移除设备成功',
            'data' => $result
        ];
    }

    /**
     * 获取设备组列表
     */
    public function getDeviceGroupList(string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->device->groupList();

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

    /**
     * 删除设备组
     */
    public function deleteDeviceGroup(int $groupId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->device->groupDelete($groupId);

        return [
            'code' => 'SUCCESS',
            'message' => '删除设备组成功',
            'data' => $result
        ];
    }

    /**
     * 获取设备组中的设备列表
     */
    public function getDeviceInGroup(int $groupId, int $page = 1, int $size = 10, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->device->groupDevices($groupId, $page, $size);

        return [
            'code' => 'SUCCESS',
            'message' => '获取设备组中的设备列表成功',
            'data' => $result
        ];
    }

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