// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()
    const openid = wxContext.OPENID

    const { roomId, name, meetTime, keywords } = event

    if (!roomId) {
        return {
            success: false,
            msg: '房间ID不能为空'
        }
    }

    try {
        // 构建更新数据
        const updateData = {}
        if (name !== undefined) {
            updateData.name = name
        }
        if (meetTime !== undefined) {
            updateData.meetTime = meetTime
        }
        if (keywords !== undefined) {
            updateData.keywords = keywords
        }

        // 更新房间信息
        await db.collection('rooms').doc(roomId).update({
            data: updateData
        })

        return {
            success: true,
            msg: '更新成功'
        }
    } catch (err) {
        console.error(err)
        return {
            success: false,
            msg: '更新失败',
            error: err
        }
    }
}

