package jnpf.culture.util;

import org.apache.commons.lang3.tuple.MutablePair;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

/**
 * 日期工具
 *
 * @author yanwenfu
 * @create 2026-03-23
 */
public class DateRangeUtil {

    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    public static final String QUERY_DAY = "day";
    public static final String QUERY_WEEK = "week";
    public static final String QUERY_MONTH = "month";

    /**
     * @param dateStr   日期字符串 yyyy-MM-dd
     * @param queryType day / week / month
     * @return MutablePair<开始日期, 结束日期>
     */
    public static MutablePair<String, String> getRange(String dateStr, String queryType) {

        LocalDate date = LocalDate.parse(dateStr, FORMATTER);
        switch (queryType.toLowerCase()) {

            case QUERY_DAY:
                return MutablePair.of(dateStr, dateStr);
            case QUERY_WEEK:
                // 周一为一周开始，周日为结束
                LocalDate weekStart = date.with(DayOfWeek.MONDAY);
                LocalDate weekEnd = date.with(DayOfWeek.SUNDAY);
                return MutablePair.of(
                        weekStart.format(FORMATTER),
                        weekEnd.format(FORMATTER)
                );
            case QUERY_MONTH:
                LocalDate monthStart = date.with(TemporalAdjusters.firstDayOfMonth());
                LocalDate monthEnd = date.with(TemporalAdjusters.lastDayOfMonth());
                return MutablePair.of(
                        monthStart.format(FORMATTER),
                        monthEnd.format(FORMATTER)
                );
            default:
                throw new IllegalArgumentException("queryType 只能是 day/week/month");
        }
    }
}
