package jnpf.attendance.schedule;

import org.slf4j.Logger;

/**
 * 将 {@link ShiftPlanAssignmentResult} / {@link ShiftPlanPickSnapshot} 写入日志。
 *
 * <p><b>线上（仅 ERROR 可见时）</b>：里程碑与 shortage / incomplete 说明均经 {@link AutoSchedulePipelineLog} 以 ERROR 输出；明细全文亦用 ERROR（体量大时请依赖 logger 过滤）。
 */
public final class ShiftPlanAssignmentResultLogger {

    private ShiftPlanAssignmentResultLogger() {}

    /** @see #logPickSnap(Logger, ShiftPlanPickSnapshot) */
    public static void logInfo(Logger log, ShiftPlanAssignmentResult result) {
        logPickSnap(log, result);
    }

    public static void logPickSnap(Logger log, ShiftPlanAssignmentResult result) {
        logPickSnap(log, ShiftPlanPickSnapshot.from(result));
    }

    public static void logPickSnap(Logger log, ShiftPlanPickSnapshot pickSnap) {
        if (log == null) {
            return;
        }
        if (pickSnap == null) {
            return;
        }
        int noteCount = pickSnap.getIncompleteScheduleReasons().size();
        if (pickSnap.isEmpty()) {
            if (log.isErrorEnabled()) {
                if (noteCount > 0) {
                    AutoSchedulePipelineLog.info(
                            log,
                            AutoSchedulePipelineLog.PHASE_PICK_SUMMARY,
                            "EMPTY_ROSTER | shift blocks were all dropped or produced no assignment | incompleteNotes="
                                    + noteCount);
                } else {
                    AutoSchedulePipelineLog.info(
                            log,
                            AutoSchedulePipelineLog.PHASE_PICK_SUMMARY,
                            "EMPTY | no roster rows after staff assignment");
                }
            }
        } else {
            int picked = pickSnap.countAssignedStaff();
            int need = pickSnap.countTotalNeed();
            if (log.isErrorEnabled()) {
                AutoSchedulePipelineLog.info(
                        log,
                        AutoSchedulePipelineLog.PHASE_PICK_SUMMARY,
                        String.format(
                                "shiftBlocks=%d employees=%d picked=%d need=%d complete=%s incompleteNotes=%d",
                                pickSnap.getByShift().size(),
                                pickSnap.getByEmployee().size(),
                                picked,
                                need,
                                need <= 0 || picked >= need,
                                noteCount));
            }
            if (picked < need) {
                AutoSchedulePipelineLog.warn(
                        log,
                        AutoSchedulePipelineLog.PHASE_PICK_SUMMARY,
                        "SHORTAGE deficit="
                                + (need - picked)
                                + " (enable ERROR for AutoScheduleService / ShiftPlanAssignmentResultLogger to inspect)");
            }
            if (noteCount > 0) {
                AutoSchedulePipelineLog.warn(
                        log,
                        AutoSchedulePipelineLog.PHASE_PICK_SUMMARY,
                        "INCOMPLETE explanatory notes="
                                + noteCount
                                + " (inspect ShiftPlanAssignmentResult.getIncompleteScheduleReasons or pickSnap ERROR detail)");
            }
        }
        if (!log.isErrorEnabled()) {
            return;
        }
        log.error(
                "{} pickSnap detail:\n{}",
                AutoSchedulePipelineLog.MARKER,
                pickSnap.formatPickSnapText());
    }

    /**
     * 完整拼接文本（与 {@link #logPickSnap} 末尾明细一致）；在仅开启 ERROR 时用于补打大块详情。
     */
    public static void logDebug(Logger log, ShiftPlanAssignmentResult result) {
        if (log == null || !log.isErrorEnabled()) {
            return;
        }
        ShiftPlanPickSnapshot pickSnap = ShiftPlanPickSnapshot.from(result);
        log.error(
                "{} pickSnap detail:\n{}",
                AutoSchedulePipelineLog.MARKER,
                pickSnap.formatPickSnapText());
    }
}
