package com.cameleer.server.app.dto;

import com.cameleer.server.core.admin.AppSettings;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

@Schema(description = "Per-application dashboard settings")
public record AppSettingsRequest(
        @NotNull @Min(1)
        @Schema(description = "SLA duration threshold in milliseconds")
        Integer slaThresholdMs,

        @NotNull @Min(0) @Max(100)
        @Schema(description = "Error rate % threshold for warning (yellow) health dot")
        Double healthErrorWarn,

        @NotNull @Min(0) @Max(100)
        @Schema(description = "Error rate % threshold for critical (red) health dot")
        Double healthErrorCrit,

        @NotNull @Min(0) @Max(100)
        @Schema(description = "SLA compliance % threshold for warning (yellow) health dot")
        Double healthSlaWarn,

        @NotNull @Min(0) @Max(100)
        @Schema(description = "SLA compliance % threshold for critical (red) health dot")
        Double healthSlaCrit
) {

    public AppSettings toSettings(String appId, String environment) {
        Instant now = Instant.now();
        return new AppSettings(appId, environment, slaThresholdMs, healthErrorWarn, healthErrorCrit,
                healthSlaWarn, healthSlaCrit, now, now);
    }

    public List<String> validate() {
        List<String> errors = new ArrayList<>();
        if (healthErrorWarn != null && healthErrorCrit != null
                && healthErrorWarn > healthErrorCrit) {
            errors.add("healthErrorWarn must be <= healthErrorCrit");
        }
        if (healthSlaWarn != null && healthSlaCrit != null
                && healthSlaWarn < healthSlaCrit) {
            errors.add("healthSlaWarn must be >= healthSlaCrit (higher SLA = healthier)");
        }
        return errors;
    }
}
