package com.usky.vpp.util;

import cn.hutool.core.date.DateUtil;
import io.github.shawyeok.chinese.workday.WorkdayCalendar;

import java.time.LocalDate;
import java.util.Date;

/**
 * 工作日 / 非工作日判定（法定节假日 + 调休）
 * <p>Hutool {@link DateUtil} 负责日期转换；节假日数据由 chinese-workday-calendar 提供
 * （Hutool core 未内置 {@code ChineseHoliday}）。</p>
 */
public final class VppWorkdayHelper {

    public static final String DAY_TYPE_WORKDAY = "WORKDAY";
    public static final String DAY_TYPE_NON_WORKDAY = "NON_WORKDAY";

    private static final WorkdayCalendar WORKDAY_CALENDAR = WorkdayCalendar.builder().build();

    private VppWorkdayHelper() {
    }

    /**
     * 是否为工作日（含调休上班，排除法定节假日与正常周末）
     */
    public static boolean isWorkday(LocalDate date) {
        if (date == null) {
            return false;
        }
        Date targetDate = DateUtil.date(date);
        boolean isHoliday = isRestDay(targetDate, date);
        boolean isAdjustWork = isMakeupWorkday(targetDate, date);
        return !isHoliday || isAdjustWork;
    }

    /**
     * 是否为非工作日（周末或法定节假日，不含调休上班）
     */
    public static boolean isNonWorkday(LocalDate date) {
        return !isWorkday(date);
    }

    public static String resolveDayType(LocalDate date) {
        return isWorkday(date) ? DAY_TYPE_WORKDAY : DAY_TYPE_NON_WORKDAY;
    }

    public static String resolveDayTypeLabel(LocalDate date) {
        return isWorkday(date) ? "工作日" : "非工作日";
    }

    /** 是否为休息日（法定节假日或正常周末，不含调休上班） */
    private static boolean isRestDay(Date targetDate, LocalDate localDate) {
        return !WORKDAY_CALENDAR.isWorkday(localDate);
    }

    /** 是否为调休上班日（周末补班） */
    private static boolean isMakeupWorkday(Date targetDate, LocalDate localDate) {
        return DateUtil.isWeekend(targetDate) && WORKDAY_CALENDAR.isWorkday(localDate);
    }
}
