package com.fs.task.utils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.WeekFields;
import java.util.Locale;

public class RunKeyUtil {

    private static final DateTimeFormatter DAY = DateTimeFormatter.ofPattern("yyyyMMdd");
    private static final DateTimeFormatter MONTH = DateTimeFormatter.ofPattern("yyyyMM");

    // 1=天 2=周 3=月
    public static String buildRunKey(Integer frequencyUnit, LocalDate date) {
        if (frequencyUnit == null) return "UNKNOWN";
        if (frequencyUnit == 1) {
            return "D_" + date.format(DAY);
        }
        if (frequencyUnit == 2) {
            WeekFields wf = WeekFields.ISO;
            int weekBasedYear = date.get(wf.weekBasedYear());
            int week = date.get(wf.weekOfWeekBasedYear());
            return String.format("W_%dW%02d", weekBasedYear, week);
        }
        if (frequencyUnit == 3) {
            return "M_" + date.format(MONTH);
        }
        return "UNKNOWN";
    }
}
