package jnpf.attendance.schedule;

import java.io.Serializable;
import java.util.Objects;

/**
 * 第六步：候选固定班次统计与展示字段。
 */
public final class PostFixedShiftCandidate implements Serializable {

    private final String postId;
    /** 半小时对齐后的展示标签，如 {@code 09:00-12:00} */
    private final String normalizedShiftLabel;
    private final int startMinuteInclusive;
    private final int endMinuteExclusive;
    private final int appearanceDayCount;
    private final int appearanceCount;
    private final int similarHistoryDayTotal;
    /**
     * 历史班次在 {@link jnpf.model.attendance.vo.ShiftPostStatVo#getShiftId()} 上的快照（与考勤组 {@link jnpf.model.attendance.vo.ShiftPeriodVo#getId()}
     * 同源时可走 ID 直配，跳过时段阈值匹配）。
     */
    private final String historicalAttendanceShiftId;

    public PostFixedShiftCandidate(
            String postId,
            String normalizedShiftLabel,
            int startMinuteInclusive,
            int endMinuteExclusive,
            int appearanceDayCount,
            int appearanceCount,
            int similarHistoryDayTotal) {
        this(postId, normalizedShiftLabel, startMinuteInclusive, endMinuteExclusive, appearanceDayCount, appearanceCount,
                similarHistoryDayTotal, null);
    }

    public PostFixedShiftCandidate(
            String postId,
            String normalizedShiftLabel,
            int startMinuteInclusive,
            int endMinuteExclusive,
            int appearanceDayCount,
            int appearanceCount,
            int similarHistoryDayTotal,
            String historicalAttendanceShiftId) {
        this.postId = postId == null ? "" : postId;
        this.normalizedShiftLabel =
                normalizedShiftLabel == null ? "" : normalizedShiftLabel;
        this.startMinuteInclusive = startMinuteInclusive;
        this.endMinuteExclusive = endMinuteExclusive;
        this.appearanceDayCount = Math.max(0, appearanceDayCount);
        this.appearanceCount = Math.max(0, appearanceCount);
        this.similarHistoryDayTotal = Math.max(0, similarHistoryDayTotal);
        this.historicalAttendanceShiftId =
                historicalAttendanceShiftId == null || historicalAttendanceShiftId.isBlank()
                        ? null
                        : historicalAttendanceShiftId.trim();
    }

    public String getPostId() {
        return postId;
    }

    public String getNormalizedShiftLabel() {
        return normalizedShiftLabel;
    }

    public int getStartMinuteInclusive() {
        return startMinuteInclusive;
    }

    public int getEndMinuteExclusive() {
        return endMinuteExclusive;
    }

    public int getAppearanceDayCount() {
        return appearanceDayCount;
    }

    public int getAppearanceCount() {
        return appearanceCount;
    }

    public int getSimilarHistoryDayTotal() {
        return similarHistoryDayTotal;
    }

    public String getHistoricalAttendanceShiftId() {
        return historicalAttendanceShiftId;
    }

    /** 出现天数 / 相似历史日总天数（0～1）。 */
    public double getAppearanceDayRatio() {
        if (similarHistoryDayTotal <= 0) {
            return 0d;
        }
        return appearanceDayCount / (double) similarHistoryDayTotal;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        PostFixedShiftCandidate that = (PostFixedShiftCandidate) o;
        return startMinuteInclusive == that.startMinuteInclusive
                && endMinuteExclusive == that.endMinuteExclusive
                && appearanceDayCount == that.appearanceDayCount
                && appearanceCount == that.appearanceCount
                && similarHistoryDayTotal == that.similarHistoryDayTotal
                && Objects.equals(postId, that.postId)
                && Objects.equals(normalizedShiftLabel, that.normalizedShiftLabel)
                && Objects.equals(historicalAttendanceShiftId, that.historicalAttendanceShiftId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(
                postId,
                normalizedShiftLabel,
                startMinuteInclusive,
                endMinuteExclusive,
                historicalAttendanceShiftId);
    }
}
