package dev.sonpx.loyalty.agent.core.executor;

/**
 * Capability scopes used to keep each agent's tool surface small and safe.
 */
public enum AgentToolScope {
    ALL,
    READ_ONLY,
    CREATE_CAMPAIGN,
    CREATE_RULE;

    public boolean allows(String toolName) {
        ToolCapability capability = ToolCapability.fromToolName(toolName);
        return switch (this) {
            case ALL -> true;
            case READ_ONLY -> capability == ToolCapability.READ || capability == ToolCapability.VALIDATE;
            case CREATE_CAMPAIGN -> capability == ToolCapability.CAMPAIGN_WRITE
                    || capability == ToolCapability.CAMPAIGN_GENERATE_ID
                    || capability == ToolCapability.VALIDATE
                    || capability == ToolCapability.READ;
            case CREATE_RULE -> capability == ToolCapability.RULE_WRITE
                    || capability == ToolCapability.RULE_GENERATE_ID
                    || capability == ToolCapability.VALIDATE
                    || capability == ToolCapability.READ;
        };
    }

    private enum ToolCapability {
        READ,
        VALIDATE,
        CAMPAIGN_GENERATE_ID,
        RULE_GENERATE_ID,
        CAMPAIGN_WRITE,
        RULE_WRITE,
        OTHER;

        static ToolCapability fromToolName(String toolName) {
            if (toolName == null) {
                return OTHER;
            }

            String normalized = toolName.toLowerCase();
            if (normalized.contains("createcampaign")) {
                return CAMPAIGN_WRITE;
            }
            if (normalized.contains("createrule")) {
                return RULE_WRITE;
            }
            if (normalized.contains("generatecampaignid")) {
                return CAMPAIGN_GENERATE_ID;
            }
            if (normalized.contains("generateruleid")) {
                return RULE_GENERATE_ID;
            }
            if (normalized.startsWith("check") || normalized.contains("_check")) {
                return VALIDATE;
            }
            if (normalized.startsWith("search") || normalized.contains("_search")
                    || normalized.startsWith("count") || normalized.contains("_count")
                    || normalized.startsWith("get") || normalized.contains("_get")) {
                return READ;
            }
            return OTHER;
        }
    }
}
