package com.easyagents.federation.sql.adapter;

import com.easyagents.federation.sql.source.FederationSourceDefinition;
import java.sql.Connection;
import java.time.Instant;
import java.util.Objects;

/**
 * Adapter 采集数据库目录统计时使用的只读上下文。
 *
 * @param sourceDefinition 当前物理数据源定义
 * @param connection 已从运行时连接池借出的 JDBC 连接
 * @param collectedAt 本轮统计采集时间
 * @param expiresAt 本轮统计默认失效时间
 * @param queryTimeoutSeconds 单条目录查询超时秒数
 */
public record FederationStatisticsCollectionContext(
    FederationSourceDefinition sourceDefinition,
    Connection connection,
    Instant collectedAt,
    Instant expiresAt,
    int queryTimeoutSeconds
) {

    /**
     * 校验统计采集上下文。
     */
    public FederationStatisticsCollectionContext {
        sourceDefinition = Objects.requireNonNull(sourceDefinition, "sourceDefinition");
        connection = Objects.requireNonNull(connection, "connection");
        collectedAt = Objects.requireNonNull(collectedAt, "collectedAt");
        expiresAt = Objects.requireNonNull(expiresAt, "expiresAt");
        if (!expiresAt.isAfter(collectedAt)) {
            throw new IllegalArgumentException("expiresAt must be after collectedAt");
        }
        if (queryTimeoutSeconds <= 0) {
            throw new IllegalArgumentException("queryTimeoutSeconds must be positive");
        }
    }
}
