package tech.easyflow.agent.config;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.StringUtils;
import org.springframework.util.unit.DataSize;

import java.time.Duration;

/**
 * Agent 单会话工作区的路径、配额与保留配置。
 */
@ConfigurationProperties(prefix = "easyflow.agent.workspace")
public class AgentWorkspaceProperties implements InitializingBean {

    private String root = "./agent-workspaces";
    private DataSize maxTotalSize = DataSize.ofMegabytes(512);
    private DataSize maxSingleFileSize = DataSize.ofMegabytes(100);
    private int maxFileCount = 2_000;
    private DataSize maxReadSize = DataSize.ofMegabytes(2);
    private Duration retention = Duration.ofHours(24);
    private Duration cleanupInterval = Duration.ofMinutes(30);

    /** @return 工作区根目录 */
    public String getRoot() { return root; }
    /** @param root 工作区根目录 */
    public void setRoot(String root) { this.root = root; }
    /** @return 单会话工作区总量上限 */
    public DataSize getMaxTotalSize() { return maxTotalSize; }
    /** @param maxTotalSize 单会话工作区总量上限 */
    public void setMaxTotalSize(DataSize maxTotalSize) { this.maxTotalSize = maxTotalSize; }
    /** @return 单文件大小上限 */
    public DataSize getMaxSingleFileSize() { return maxSingleFileSize; }
    /** @param maxSingleFileSize 单文件大小上限 */
    public void setMaxSingleFileSize(DataSize maxSingleFileSize) { this.maxSingleFileSize = maxSingleFileSize; }
    /** @return 文件数量上限 */
    public int getMaxFileCount() { return maxFileCount; }
    /** @param maxFileCount 文件数量上限 */
    public void setMaxFileCount(int maxFileCount) { this.maxFileCount = maxFileCount; }
    /** @return 单次读取大小上限 */
    public DataSize getMaxReadSize() { return maxReadSize; }
    /** @param maxReadSize 单次读取大小上限 */
    public void setMaxReadSize(DataSize maxReadSize) { this.maxReadSize = maxReadSize; }
    /** @return 工作区保留期 */
    public Duration getRetention() { return retention; }
    /** @param retention 工作区保留期 */
    public void setRetention(Duration retention) { this.retention = retention; }
    /** @return 清理周期 */
    public Duration getCleanupInterval() { return cleanupInterval; }
    /** @param cleanupInterval 清理周期 */
    public void setCleanupInterval(Duration cleanupInterval) { this.cleanupInterval = cleanupInterval; }

    /**
     * 启动期校验工作区配置，避免以无界或互相矛盾的限制启动。
     */
    @Override
    public void afterPropertiesSet() {
        if (!StringUtils.hasText(root)) {
            throw new IllegalStateException("easyflow.agent.workspace.root 不能为空");
        }
        requirePositive(maxTotalSize, "max-total-size");
        requirePositive(maxSingleFileSize, "max-single-file-size");
        requirePositive(maxReadSize, "max-read-size");
        if (maxTotalSize.toBytes() < maxSingleFileSize.toBytes()) {
            throw new IllegalStateException("工作区总量上限不能小于单文件大小上限");
        }
        if (maxReadSize.toBytes() > maxSingleFileSize.toBytes()) {
            throw new IllegalStateException("工作区读取上限不能大于单文件大小上限");
        }
        if (maxFileCount <= 0 || !positive(retention) || !positive(cleanupInterval)) {
            throw new IllegalStateException("工作区文件数量、保留期和清理周期必须为正值");
        }
    }

    private void requirePositive(DataSize value, String name) {
        if (value == null || value.toBytes() <= 0) {
            throw new IllegalStateException("easyflow.agent.workspace." + name + " 必须为正值");
        }
    }

    private boolean positive(Duration value) {
        return value != null && !value.isZero() && !value.isNegative();
    }
}
