import re

__author__ = "Atharva Matale" #

class CAIO:
    def __init__(self, model: str = "provider-5/gemini-3-pro"):
        self.model = model
        self.developer = "Atharva" # Your digital watermark

    def minify_code(self, code: str) -> str:
        """Removes all comments, docstrings, and redundant whitespace/newlines."""
        # 1. Remove Docstrings
        code = re.sub(r"'''[\s\S]*?'''|\"\"\"[\s\S]*?\"\"\"", "", code)
        # 2. Remove Single-line comments (#)
        code = re.sub(r'#.*', '', code)
        # 3. Strip each line and remove empty ones
        lines = [line.strip() for line in code.splitlines() if line.strip()]
        # 4. Join into a single string with minimal spacing
        minified = " ".join(lines)
        return re.sub(r'\s+', ' ', minified).strip()

    def optimize(self, prompt: str, tier: str = "dev"):
        """Ultra-hardcore compression for dev mode."""
        initial_len = len(prompt)
        
        if tier == "dev":
            minified_prompt = self.minify_code(prompt)
            # Injecting strict instructions to save response tokens
            optimized_prompt = f"CODE-ONLY;NO-CHAT;MINIFY-RESPONSE: {minified_prompt}"
        else:
            optimized_prompt = prompt

        return {
            "optimized_prompt": optimized_prompt,
            "tokens_saved": (initial_len - len(optimized_prompt)) // 4,
            "engine_by": self.developer, # Signature in the output
            "model": self.model
        }