package com.easyagents.scheduler;

import java.time.Instant;
import java.util.Map;
import java.util.Objects;

/**
 * Handler 执行时可读取的稳定触发上下文。
 *
 * @param scheduleId 任务标识
 * @param handlerCode Handler Code
 * @param scheduledFireTime 计划触发时间
 * @param actualFireTime 实际触发时间
 * @param fireInstanceId 当前物理触发实例标识
 * @param invocationId 立即触发调用标识，周期触发时可为空
 * @param recovering 当前执行是否由节点故障恢复产生
 * @param parameters 合并后的不可变字符串参数
 */
public record ScheduleFireContext(
    ScheduleId scheduleId,
    String handlerCode,
    Instant scheduledFireTime,
    Instant actualFireTime,
    String fireInstanceId,
    String invocationId,
    boolean recovering,
    Map<String, String> parameters
) {

    /**
     * 校验并创建触发上下文。
     *
     * @throws NullPointerException 必填字段为空时抛出
     */
    public ScheduleFireContext {
        scheduleId = Objects.requireNonNull(scheduleId, "scheduleId must not be null");
        handlerCode = Objects.requireNonNull(handlerCode, "handlerCode must not be null");
        scheduledFireTime = Objects.requireNonNull(
            scheduledFireTime,
            "scheduledFireTime must not be null"
        );
        actualFireTime = Objects.requireNonNull(actualFireTime, "actualFireTime must not be null");
        fireInstanceId = Objects.requireNonNull(fireInstanceId, "fireInstanceId must not be null");
        parameters = ScheduleDefinition.copyAndValidateParameters(parameters);
    }
}
