package com.ruoyi.cc.utils;

import java.util.regex.Pattern;

/**
 * FreeSWITCH name safety for ESL api args and conf path segments.
 * Rejects whitespace/CRLF/quotes that can break sofia command framing,
 * while allowing common profile names such as internal, external, my_profile-1.
 */
public final class FsEslNameUtils {

    private static final Pattern SAFE_NAME =
            Pattern.compile("^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$");

    /** Call/FS uuid used only for String.contains filter (no shell). */
    private static final Pattern SAFE_LOG_QUERY_ID =
            Pattern.compile("^[A-Za-z0-9][A-Za-z0-9_-]{10,63}$");

    private FsEslNameUtils() {
    }

    /**
     * @return true if name is safe to embed in sofia ESL arguments
     */
    public static boolean isSafeName(String name) {
        return name != null && SAFE_NAME.matcher(name).matches();
    }

    /**
     * Safe id for log line filtering (FS/call uuid). Blocks shell metacharacters
     * from legacy "sh -c ... grep '"+uuid+"'" style bugs; allows hyphenated UUIDs.
     */
    public static boolean isSafeLogQueryId(String id) {
        return id != null && SAFE_LOG_QUERY_ID.matcher(id).matches();
    }
}
