import os
import json
from datetime import datetime, timedelta

filepath = os.path.dirname(__file__).replace("\\", "/")
data_path = filepath+'/deer_pipe/'


# 记录信息
async def record_month(uid, day: int):
    # 获取当前日期
    current_date = datetime.now()
    formatted_date = current_date.strftime('%Y-%m')
    json_file_path = f"{data_path}/{formatted_date}.json"
    if os.path.exists(json_file_path):
        # 如果文件存在，则读取其中的数据
        with open(json_file_path, 'r', encoding='utf-8') as file:
            data = json.load(file)
            if uid in data:
                day_list = data[uid]
                day_list.append(day)
            else:
                day_list = [day]
            data[uid] = day_list
    else:
        # 如果文件不存在，则创建一个新的空数据结构
        day_list = [day]
        data = {'uid': day_list}
    with open(json_file_path, 'w', encoding='utf-8') as file:
        json.dump(data, file, ensure_ascii=False, indent=4)


# 读取当月信息
async def get_records(uid):
    day_list = []
    # 获取当前日期
    current_date = datetime.now()
    formatted_date = current_date.strftime('%Y-%m')
    json_file_path = f"{data_path}/{formatted_date}.json"
    if os.path.exists(json_file_path):
        # 如果文件存在，则读取其中的数据
        with open(json_file_path, 'r', encoding='utf-8') as file:
            data = json.load(file)
            if uid in data:
                day_list = data[uid]
    else:
        day_list = [0]
    return day_list
