package com.aps.macroplanner.data;

import java.time.LocalDate;

/**
 * 周期 (Period) — 对应 Quintiq 中的 Period_MP
 *
 * <p>startDate 用于 leadTime 日期偏移计算:
 * 生产在周期 t, 入库在 t+leadTimeDays 天, 通过 startDate 精确定位到目标周期。</p>
 *
 * <p>durationInDays 用于安全库存天数计算:
 * 当库存规格以"天"为单位时, 需要知道每个周期的长度来折算比例。</p>
 */
public class Period {
    private final int index;
    private final String name;
    /** 周期长度 (天), 默认 1.0。用于安全库存天数折算。 */
    private final double durationInDays;
    /** 周期起始日期, 用于 leadTime 日期偏移计算。null 表示未指定(兼容旧数据)。 */
    private final LocalDate startDate;

    public Period(int index, String name) {
        this(index, name, 1.0, null);
    }

    public Period(int index, String name, double durationInDays) {
        this(index, name, durationInDays, null);
    }

    public Period(int index, String name, LocalDate startDate) {
        this(index, name, 1.0, startDate);
    }

    public Period(int index, String name, double durationInDays, LocalDate startDate) {
        this.index = index;
        this.name = name;
        this.durationInDays = durationInDays;
        this.startDate = startDate;
    }

    public int getIndex() { return index; }
    public String getName() { return name; }
    public double getDurationInDays() { return durationInDays; }
    public LocalDate getStartDate() { return startDate; }

    /**
     * 判断给定日期是否落在本周期内。
     * 周期区间: [startDate, nextPeriod.startDate) 或 [startDate, startDate + durationInDays)
     */
    public boolean contains(LocalDate date) {
        if (startDate == null || date == null) return false;
        LocalDate end = startDate.plusDays((long) durationInDays);
        return !date.isBefore(startDate) && date.isBefore(end);
    }

    @Override
    public String toString() {
        return name;
    }
}