import request from '@/common/system/request';

/**
 * 用户是否可以创建预约（仅校验，不创建）
 * @param {Object} reservationData 创建预约所需参数
 * @returns {Promise<{canCreate:boolean,message?:string,code?:number}>}
 */
export const canCreateSQReservation = async (reservationData) => {
    const res = await request.post("sq/CanCreateSQReservation", reservationData);
    if (res && typeof res.code !== 'undefined') {
        if (res.code === 0) {
            return { canCreate: true, code: 0 };
        }
        return { canCreate: false, message: res.msg || '不可创建', code: res.code };
    }
    return { canCreate: false, message: '网络异常', code: 500 };
}

/**
 * 获取预约记录
 * @param {number} index 
 * @param {number} size 
 * @returns 
 */
export const getReservationList = async (index = 1, size = 20) => {
    console.log('getReservationList', index, size);

    const res = await request.getOrCache("sq/GetReservationList", { pageIndex: index, pageSize: size }, 1);
    if (res.code == 0) {
        return res.data;
    }
    return null;
}

/**
 * 根据预约ID获取详情（结构同首页预约列表单项）
 * @param {number} id 预约ID
 * @returns {Promise<any>}
 */
export const getReservationDetail = async (id) => {
	const res = await request.get("sq/GetReservationDetail", { id: id });
	if (res.code == 0) {
		return res.data;
	}
	return null;
}

/**
 * 获取我的预约记录
 * @param {number} type 0 参与者，1发起者
 * @param {number} index 起始页
 * @param {number} size 页大小
 * @returns {Promise<any>}
 */
export const getMyReservation = async (type = 0, index = 1, size = 10) => {
    const res = await request.get("sq/GetMyReservation", {
        type: type,
        index: index,
        size: size
    });
    if (res.code == 0) {
        return res.data;
    }
    return null;
}

/**
 * 获取正在进行的预约
 * @returns {Promise<any>}
 */
export const getMyUseReservation = async () => {
    const res = await request.get("sq/GetMyUseReservation");
    if (res.code == 0) {
        return res.data;
    }
    return null;
}

/**
 * 获取预约评价
 * @param {number} reId 预约ID
 * @returns {Promise<any>}
 */
export const getEvaluateServices = async (reId) => {
    const res = await request.get("sq/GetEvaluateServices", { reId: reId });
    if (res.code == 0) {
        return res.data;
    }
    return null;
}

/**
 * 添加预约评价
 * @param {Object} evaluate 评价数据
 * @param {number} evaluate.reservation_id 预约ID
 * @param {number} evaluate.to_user_id 被评价用户ID
 * @param {number} evaluate.play_level 游戏水平评分
 * @param {number} evaluate.skills_level 技能水平评分
 * @returns {Promise<any>}
 */
export const addEvaluateServices = async (evaluate) => {
    const res = await request.post("sq/AddEvaluateServices", evaluate);
    if (res.code == 0) {
        return true;
    }
    return false;
}

/**
 * 获取我的声誉记录
 * @param {number} pageIndex 起始页
 * @param {number} pageSize 页大小
 * @returns {Promise<any>} [{title:"标题",reputation_value:"变化值","created_at":"变化时间"}]
 */
export const getReputationByUser = async (pageIndex = 1, pageSize = 20) => {
    const res = await request.get("sq/GetReputationByUser", {
        pageIndex: pageIndex,
        pageSize: pageSize
    });
    if (res.code == 0) {
        //data:
        return res.data;
    }
    return null;
}

/**
 * 获取支付记录
 * @param {number} pageIndex 起始页
 * @param {number} pageSize 页大小
 * @returns {Promise<any>} 返回支付记录数据
 */
export const getPaymentRecords = async (pageIndex = 1, pageSize = 20) => {
    const res = await request.get("sq/GetPaymentRecords", {
        pageIndex: pageIndex,
        pageSize: pageSize
    });
    if (res.code == 0) {
        return res.data;
    }
    return null;
}

/**
 * 获取可预约的房间列表
 * @param {number} startTime 开始时间 时间戳(秒)
 * @param {number} endTime 结束时间 时间戳(秒)
 * @returns {Promise<any>} [{id:"房间id",name:"房间名称"}]
 */
export const getReservationRoomList = async (startTime, endTime) => {
    const res = await request.get("sq/GetReservationRoomList", {
        startTime: startTime,
        endTime: endTime
    });
    if (res.code == 0) {
        return res.data;
    }
    return null;
}

/**
 * 用户创建预约接口
 * @param {Object} reservationData 预约数据
 * @param {number} reservationData.room_id 房间ID
 * @param {number} reservationData.start_time 开始时间 时间戳(秒)
 * @param {number} reservationData.end_time 结束时间 时间戳(秒)
 * @param {string} reservationData.game_type 游戏类型
 * @param {number} reservationData.deposit_fee 押金费用
 * @param {string} reservationData.important_data 重要数据
 * @returns {Promise<any>} 返回预约结果
 */
export const addSQReservation = async (reservationData) => {
    const res = await request.post("sq/AddSQReservation", reservationData);
    if (res.code == 0) {
        return {
            success: true,
            data: res.data.reservation_id
        };
    }
    return {
        success: false,
        message: res.msg || '预约失败'
    };
}

/**
 * 用户加入预约接口
 * @param {Object} joinData 加入预约数据
 * @param {number} joinData.ReservationsId 预约ID
 * @param {string} joinData.important_data 重要数据
 * @returns {Promise<any>} 返回加入结果
 */
export const joinReservation = async (joinData) => {
    const res = await request.post("sq/JoinReservation", joinData);
    if (res.code == 0) {
        return {
            success: true,
            message: res.msg || '加入预约成功',
            data: res.data.reservation_id
        };
    }
    return {
        success: false,
        message: res.msg || '加入预约失败'
    };
}

/**
 * 取消预约接口
 * @param {Object} cancelData 取消预约数据
 * @param {number} cancelData.reservation_id 预约ID
 * @param {string} cancelData.cancel_reason 取消原因（可选）
 * @returns {Promise<any>} 返回取消结果
 */
export const cancelReservation = async (reservation_id, cancel_reason) => {
    console.log("cancelReservation", reservation_id, cancel_reason);
    const res = await request.post("sq/CancelReservation", { reservation_id: reservation_id, cancel_reason: cancel_reason });
    if (res.code == 0) {
        return {
            success: true,
            message: res.msg || '取消预约成功'
        };
    }
    return {
        success: false,
        message: res.msg || '取消预约失败'
    };
}

/**
 * 预约签到接口（仅发起者可操作，且只能签到一次）
 * @param {Object} checkInData 签到数据
 * @param {number} checkInData.reservation_id 预约ID
 * @param {Array} checkInData.attendeds 参会名单（不包含发起者）
 * @param {number} checkInData.attendeds[].user_id 用户ID
 * @param {boolean} checkInData.attendeds[].isAttended 是否到场
 * @returns {Promise<any>} 返回签到结果
 */
export const checkInReservation = async (checkInData) => {
    console.log("checkInReservation", checkInData);
    const res = await request.post("sq/CheckInReservation", checkInData);
    if (res.code == 0) {
        return {
            success: true,
            message: res.msg || '签到成功'
        };
    }
    return {
        success: false,
        message: res.msg || '签到失败'
    };
}




export const sqInterface = {
    canCreateSQReservation,
    getReservationList,
	getReservationDetail,
    getMyReservation,
    getMyUseReservation,
    getEvaluateServices,
    addEvaluateServices,
    getReputationByUser,
    getPaymentRecords,
    getReservationRoomList,
    addSQReservation,
    joinReservation,
    cancelReservation,
    checkInReservation
}