<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

/**
 * 微信云开发服务类
 */
final class WechatCloudService
{
    /**
     * 构造函数
     */
    public function __construct(
        private readonly Container $app
    ) {}

    /**
     * 调用云函数
     */
    public function callFunction(string $functionName, array $data, string $driver = 'miniprogram', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->cloud->invoke($functionName, $data);

        return [
            'code' => 'SUCCESS',
            'message' => '调用云函数成功',
            'data' => $result
        ];
    }

    /**
     * 获取云数据库实例
     */
    public function getDatabase(string $driver = 'miniprogram', string $name = 'default'): mixed
    {
        $app = $this->getApp($driver, $name);
        
        return $app->cloud->database();
    }

    /**
     * 云数据库查询
     */
    public function databaseQuery(string $collection, callable $callback, string $driver = 'miniprogram', string $name = 'default'): array
    {
        $db = $this->getDatabase($driver, $name);
        $collection = $db->collection($collection);
        
        $result = $callback($collection);

        return [
            'code' => 'SUCCESS',
            'message' => '云数据库查询成功',
            'data' => $result
        ];
    }

    /**
     * 云数据库新增
     */
    public function databaseAdd(string $collection, array $data, string $driver = 'miniprogram', string $name = 'default'): array
    {
        $db = $this->getDatabase($driver, $name);
        
        $result = $db->collection($collection)->add($data);

        return [
            'code' => 'SUCCESS',
            'message' => '云数据库新增成功',
            'data' => $result
        ];
    }

    /**
     * 云数据库更新
     */
    public function databaseUpdate(string $collection, array $condition, array $data, string $driver = 'miniprogram', string $name = 'default'): array
    {
        $db = $this->getDatabase($driver, $name);
        
        $result = $db->collection($collection)->where($condition)->update($data);

        return [
            'code' => 'SUCCESS',
            'message' => '云数据库更新成功',
            'data' => $result
        ];
    }

    /**
     * 云数据库删除
     */
    public function databaseDelete(string $collection, array $condition, string $driver = 'miniprogram', string $name = 'default'): array
    {
        $db = $this->getDatabase($driver, $name);
        
        $result = $db->collection($collection)->where($condition)->remove();

        return [
            'code' => 'SUCCESS',
            'message' => '云数据库删除成功',
            'data' => $result
        ];
    }

    /**
     * 云存储上传文件
     */
    public function uploadFile(string $filePath, string $cloudPath, string $driver = 'miniprogram', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->cloud->uploadFile($filePath, $cloudPath);

        return [
            'code' => 'SUCCESS',
            'message' => '云存储上传文件成功',
            'data' => $result
        ];
    }

    /**
     * 云存储下载文件
     */
    public function downloadFile(string $cloudPath, string $filePath, string $driver = 'miniprogram', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->cloud->downloadFile($cloudPath, $filePath);

        return [
            'code' => 'SUCCESS',
            'message' => '云存储下载文件成功',
            'data' => $result
        ];
    }

    /**
     * 云存储删除文件
     */
    public function deleteFile(array $fileList, string $driver = 'miniprogram', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->cloud->deleteFile($fileList);

        return [
            'code' => 'SUCCESS',
            'message' => '云存储删除文件成功',
            'data' => $result
        ];
    }

    /**
     * 云存储获取文件信息
     */
    public function getFileInfo(array $fileList, string $driver = 'miniprogram', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->cloud->getFileInfo($fileList);

        return [
            'code' => 'SUCCESS',
            'message' => '云存储获取文件信息成功',
            'data' => $result
        ];
    }

    /**
     * 云存储获取临时链接
     */
    public function getTempFileURL(array $fileList, string $driver = 'miniprogram', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->cloud->getTempFileURL($fileList);

        return [
            'code' => 'SUCCESS',
            'message' => '云存储获取临时链接成功',
            'data' => $result
        ];
    }

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