from datetime import datetime, timedelta
from lunarcalendar import Converter, Lunar  # 用于农历转换

# 为完成，需求不明确
holiday_dict = {
    "元旦": {
        "type": "fixed",
        "date": {"month": 1, "day": 1},
        "duration": 1,  # 假期天数
    },
    "春节": {
        "type": "lunar",
        "date": {"month": 1, "day": 1},  # 农历正月初一
        "duration": 7,
    },
    "清明节": {"type": "fixed", "date": {"month": 4, "day": 4}, "duration": 3},
    "劳动节": {"type": "fixed", "date": {"month": 5, "day": 1}, "duration": 5},
    "端午节": {
        "type": "lunar",
        "date": {"month": 5, "day": 5},  # 农历五月初五
        "duration": 3,
    },
    "中秋节": {
        "type": "lunar",
        "date": {"month": 8, "day": 15},  # 农历八月十五
        "duration": 1,
    },
    "国庆节": {"type": "fixed", "date": {"month": 10, "day": 1}, "duration": 7},
}


def is_holiday(year, month, day):
    current_date = datetime(year, month, day)

    # 检查是否是固定节假日
    for holiday, info in holiday_dict.items():
        if info["type"] == "fixed":
            holiday_date = datetime(year, info["date"]["month"], info["date"]["day"])
            if (current_date - holiday_date).days >= 0 and (
                current_date - holiday_date
            ).days < info["duration"]:
                print(f"今天是：{holiday}")
                return True, holiday

    # 检查是否是农历节假日
    for holiday, info in holiday_dict.items():
        if info["type"] == "lunar":
            lunar_month = info["date"]["month"]
            lunar_day = info["date"]["day"]

            # 将农历转换为公历
            lunar = Lunar(year, lunar_month, lunar_day)
            solar_date = Converter.Lunar2Solar(lunar)

            # 判断当前日期是否在农历节假日范围内
            delta = (
                current_date
                - datetime(solar_date.year, solar_date.month, solar_date.day)
            ).days
            if delta >= 0 and delta < info["duration"]:
                print(f"今天是：{holiday}")
                return True, holiday

    return False, None


def get_last_year_holiday_data(year, month, day, holiday_name):
    last_year = year - 1

    if holiday_dict[holiday_name]["type"] == "fixed":
        # 获取去年节假日的开始日期和结束日期
        start_date = datetime(
            last_year,
            holiday_dict[holiday_name]["date"]["month"],
            holiday_dict[holiday_name]["date"]["day"],
        )
        end_date = start_date + timedelta(
            days=holiday_dict[holiday_name]["duration"] - 1
        )
    else:
        lunar_month = holiday_dict[holiday_name]["date"]["month"]
        lunar_day = holiday_dict[holiday_name]["date"]["day"]

        # 将去年的农历转换为公历
        lunar = Lunar(last_year, lunar_month, lunar_day)
        solar_date = Converter.Lunar2Solar(lunar)
        start_date = datetime(solar_date.year, solar_date.month, solar_date.day)
        end_date = start_date + timedelta(
            days=holiday_dict[holiday_name]["duration"] - 1
        )

    print(f"去年节假日数据时间段: {start_date} 至 {end_date}")
    # 在这里调用数据库查询或其他方法获取去年的数据
    # last_year_data = query_data_from_db(start_date, end_date)

    return start_date, end_date


year = 2024
month = 9
day = 17
is_holiday_flag, holiday_name = is_holiday(year, month, day)
if is_holiday_flag:
    start_date, end_date = get_last_year_holiday_data(year, month, day, holiday_name)
    print(f"去年{holiday_name}的日期范围：{start_date} - {end_date}")
