package cz.kamma.llamarunner;

import java.io.File;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

/**
 * Validation utilities for configuration inputs.
 */
public class ConfigValidation {

    private static final Gson GSON = new Gson();

    /**
     * Validates numeric fields for valid ranges.
     *
     * @return a list of error messages (empty if valid)
     */
    public static java.util.List<String> validateNumericInputs(
            String host,
            int port,
            int parallel,
            int threads,
            double temperature,
            double topP,
            int topK,
            double minP,
            int ctxSize,
            int ngl) {

        java.util.List<String> errors = new java.util.ArrayList<>();

        // Validate port
        if (port < 1 || port > 65535) {
            errors.add("Port must be between 1 and 65535");
        }

        // Validate parallel
        if (parallel < 1) {
            errors.add("Parallel must be at least 1");
        }

        // Validate threads
        if (threads < 1) {
            errors.add("Threads must be at least 1");
        }

        // Validate temperature
        if (temperature < 0) {
            errors.add("Temperature must be non-negative");
        }

        // Validate topP
        if (topP < 0 || topP > 1) {
            errors.add("Top P must be between 0 and 1");
        }

        // Validate topK
        if (topK < 0) {
            errors.add("Top K must be non-negative");
        }

        // Validate minP
        if (minP < 0 || minP > 1) {
            errors.add("Min P must be between 0 and 1");
        }

        // Validate ctxSize
        if (ctxSize < 0) {
            errors.add("Context size must be non-negative");
        }

        // Validate ngl
        if (ngl < 0) {
            errors.add("GPU layers must be non-negative");
        }

        return errors;
    }

    /**
     * Validates JSON string for chatTemplateKwargs.
     *
     * @param json the JSON string to validate
     * @return error message if invalid, null if valid
     */
    public static String validateJsonKwargs(String json) {
        if (json == null || json.trim().isEmpty()) {
            return null;
        }

        try {
            GSON.fromJson(json, Object.class);
            return null;
        } catch (JsonSyntaxException e) {
            return "Invalid JSON format in kwargs";
        }
    }

    /**
     * Validates model path exists.
     *
     * @param modelPath the model file path
     * @return error message if file doesn't exist, null if valid
     */
    public static String validateModelPath(String modelPath) {
        if (modelPath == null || modelPath.trim().isEmpty()) {
            return "Model path cannot be empty";
        }

        File modelFile = new File(modelPath);
        if (!modelFile.exists()) {
            return "Model file does not exist: " + modelPath;
        }

        if (!modelFile.isFile()) {
            return "Model path is not a file: " + modelPath;
        }

        return null;
    }
}
