<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

/**
 * 微信摇一摇周边服务类
 */
final class WechatShakeAroundService
{
    /**
     * 构造函数
     */
    public function __construct(
        private readonly Container $app
    ) {}

    /**
     * 申请开通摇一摇周边功能
     */
    public function apply(string $companyName, string $phoneNumber, string $email, string $industryId, string $qualificationUrl, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->shake_around->apply($companyName, $phoneNumber, $email, $industryId, $qualificationUrl);

        return [
            'code' => 'SUCCESS',
            'message' => '申请开通摇一摇周边功能成功',
            'data' => $result
        ];
    }

    /**
     * 查看摇一摇周边功能开通状态
     */
    public function status(string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->shake_around->status();

        return [
            'code' => 'SUCCESS',
            'message' => '查看摇一摇周边功能开通状态成功',
            'data' => $result
        ];
    }

    /**
     * 新增设备
     */
    public function addDevice(array $deviceIdentifier, string $comment, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->shake_around->device->create($deviceIdentifier, $comment);

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

    /**
     * 编辑设备信息
     */
    public function updateDevice(int $deviceId, string $uuid, string $major, string $minor, string $comment, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->shake_around->device->update($deviceId, $uuid, $major, $minor, $comment);

        return [
            'code' => 'SUCCESS',
            'message' => '编辑设备信息成功',
            'data' => $result
        ];
    }

    /**
     * 查看设备列表
     */
    public function getDeviceList(int $begin = 0, int $count = 10, int $type = 0, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->shake_around->device->list($begin, $count, $type);

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

    /**
     * 批量导入设备
     */
    public function batchAddDevices(array $devices, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->shake_around->device->register($devices);

        return [
            'code' => 'SUCCESS',
            'message' => '批量导入设备成功',
            'data' => $result
        ];
    }

    /**
     * 设备分组
     */
    public function createGroup(string $groupName, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->shake_around->group->create($groupName);

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

    /**
     * 编辑分组信息
     */
    public function updateGroup(int $groupId, string $groupName, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->shake_around->group->update($groupId, $groupName);

        return [
            'code' => 'SUCCESS',
            'message' => '编辑分组信息成功',
            'data' => $result
        ];
    }

    /**
     * 删除分组
     */
    public function deleteGroup(int $groupId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->shake_around->group->delete($groupId);

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

    /**
     * 查看分组列表
     */
    public function getGroupList(int $begin = 0, int $count = 10, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->shake_around->group->list($begin, $count);

        return [
            'code' => 'SUCCESS',
            'message' => '查看分组列表成功',
            'data' => $result
        ];
    }

    /**
     * 添加设备到分组
     */
    public function addDevicesToGroup(int $groupId, array $deviceIdentifiers, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->shake_around->group->addDevice($groupId, $deviceIdentifiers);

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

    /**
     * 从分组中移除设备
     */
    public function removeDevicesFromGroup(int $groupId, array $deviceIdentifiers, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->shake_around->group->removeDevice($groupId, $deviceIdentifiers);

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

    /**
     * 查看分组中的设备列表
     */
    public function getDevicesInGroup(int $groupId, int $begin = 0, int $count = 10, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->shake_around->group->getDevices($groupId, $begin, $count);

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

    /**
     * 新增摇一摇页面
     */
    public function createPage(array $page, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->shake_around->page->create($page);

        return [
            'code' => 'SUCCESS',
            'message' => '新增摇一摇页面成功',
            'data' => $result
        ];
    }

    /**
     * 编辑摇一摇页面
     */
    public function updatePage(int $pageId, array $page, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->shake_around->page->update($pageId, $page);

        return [
            'code' => 'SUCCESS',
            'message' => '编辑摇一摇页面成功',
            'data' => $result
        ];
    }

    /**
     * 删除摇一摇页面
     */
    public function deletePage(int $pageId, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->shake_around->page->delete($pageId);

        return [
            'code' => 'SUCCESS',
            'message' => '删除摇一摇页面成功',
            'data' => $result
        ];
    }

    /**
     * 查看摇一摇页面列表
     */
    public function getPageList(int $begin = 0, int $count = 10, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->shake_around->page->list($begin, $count);

        return [
            'code' => 'SUCCESS',
            'message' => '查看摇一摇页面列表成功',
            'data' => $result
        ];
    }

    /**
     * 关联设备与页面
     */
    public function bindPage(int $pageId, array $deviceIdentifiers, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->shake_around->page->bind($pageId, $deviceIdentifiers);

        return [
            'code' => 'SUCCESS',
            'message' => '关联设备与页面成功',
            'data' => $result
        ];
    }

    /**
     * 解除设备与页面的关联
     */
    public function unbindPage(int $pageId, array $deviceIdentifiers, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->shake_around->page->unbind($pageId, $deviceIdentifiers);

        return [
            'code' => 'SUCCESS',
            'message' => '解除设备与页面的关联成功',
            'data' => $result
        ];
    }

    /**
     * 查看设备关联的页面
     */
    public function getPageByDevice(string $deviceIdentifier, string $driver = 'official_account', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->shake_around->page->getByDevice($deviceIdentifier);

        return [
            'code' => 'SUCCESS',
            'message' => '查看设备关联的页面成功',
            'data' => $result
        ];
    }

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