<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

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

    /**
     * 获取企业微信部门列表
     */
    public function getDepartmentList(int $id = 0, string $driver = 'work', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->department->list($id);

        return [
            'code' => 'SUCCESS',
            'message' => '获取企业微信部门列表成功',
            'data' => $result
        ];
    }

    /**
     * 创建企业微信部门
     */
    public function createDepartment(array $department, string $driver = 'work', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->department->create($department);

        return [
            'code' => 'SUCCESS',
            'message' => '创建企业微信部门成功',
            'data' => $result
        ];
    }

    /**
     * 更新企业微信部门
     */
    public function updateDepartment(int $id, array $department, string $driver = 'work', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->department->update($id, $department);

        return [
            'code' => 'SUCCESS',
            'message' => '更新企业微信部门成功',
            'data' => $result
        ];
    }

    /**
     * 删除企业微信部门
     */
    public function deleteDepartment(int $id, string $driver = 'work', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->department->delete($id);

        return [
            'code' => 'SUCCESS',
            'message' => '删除企业微信部门成功',
            'data' => $result
        ];
    }

    /**
     * 获取企业微信成员列表
     */
    public function getUserList(int $departmentId, bool $fetchChild = false, string $driver = 'work', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->user->list($departmentId, $fetchChild);

        return [
            'code' => 'SUCCESS',
            'message' => '获取企业微信成员列表成功',
            'data' => $result
        ];
    }

    /**
     * 获取企业微信成员详情
     */
    public function getUserDetail(string $userId, string $driver = 'work', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->user->get($userId);

        return [
            'code' => 'SUCCESS',
            'message' => '获取企业微信成员详情成功',
            'data' => $result
        ];
    }

    /**
     * 创建企业微信成员
     */
    public function createUser(array $user, string $driver = 'work', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->user->create($user);

        return [
            'code' => 'SUCCESS',
            'message' => '创建企业微信成员成功',
            'data' => $result
        ];
    }

    /**
     * 更新企业微信成员
     */
    public function updateUser(string $userId, array $user, string $driver = 'work', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->user->update($userId, $user);

        return [
            'code' => 'SUCCESS',
            'message' => '更新企业微信成员成功',
            'data' => $result
        ];
    }

    /**
     * 删除企业微信成员
     */
    public function deleteUser(string $userId, string $driver = 'work', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->user->delete($userId);

        return [
            'code' => 'SUCCESS',
            'message' => '删除企业微信成员成功',
            'data' => $result
        ];
    }

    /**
     * 批量删除企业微信成员
     */
    public function batchDeleteUser(array $userIds, string $driver = 'work', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->user->batchDelete($userIds);

        return [
            'code' => 'SUCCESS',
            'message' => '批量删除企业微信成员成功',
            'data' => $result
        ];
    }

    /**
     * 发送应用消息
     */
    public function sendAppMessage(array $message, string $driver = 'work', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->message->send($message);

        return [
            'code' => 'SUCCESS',
            'message' => '发送应用消息成功',
            'data' => $result
        ];
    }

    /**
     * 发送文本消息
     */
    public function sendTextMessage(string $userId, string $content, int $agentId, string $driver = 'work', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->message->send([
            'touser' => $userId,
            'msgtype' => 'text',
            'agentid' => $agentId,
            'text' => [
                'content' => $content,
            ],
        ]);

        return [
            'code' => 'SUCCESS',
            'message' => '发送文本消息成功',
            'data' => $result
        ];
    }

    /**
     * 发送图片消息
     */
    public function sendImageMessage(string $userId, string $mediaId, int $agentId, string $driver = 'work', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->message->send([
            'touser' => $userId,
            'msgtype' => 'image',
            'agentid' => $agentId,
            'image' => [
                'media_id' => $mediaId,
            ],
        ]);

        return [
            'code' => 'SUCCESS',
            'message' => '发送图片消息成功',
            'data' => $result
        ];
    }

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