#!/usr/bin/env python3
"""
Generate varied code review examples by injecting context into templates.
Creates 500+ unique entries from core patterns.
"""
import json
import random
from pathlib import Path

OUTPUT_FILE = Path(__file__).parent.parent / "data" / "processed" / "dev_finetuning_varied.json"

# Core patterns with context injection points
PATTERNS = [
    # Python
    {"lang": "python", "issue": "SQL Injection", 
     "bad": "def get_{entity}(id):\n    cursor.execute(f\"SELECT * FROM {table} WHERE id = {{id}}\")",
     "good": "def get_{entity}(id: int) -> dict:\n    cursor.execute(\"SELECT * FROM {table} WHERE id = %s\", (id,))"},
    {"lang": "python", "issue": "Mutable Default",
     "bad": "def {func}(items=[]):\n    items.append(new_item)\n    return items",
     "good": "def {func}(items=None):\n    if items is None:\n        items = []\n    items.append(new_item)\n    return items"},
    {"lang": "python", "issue": "String Concat in Loop",
     "bad": "def build_{entity}():\n    result = \"\"\n    for item in items:\n        result += str(item)",
     "good": "def build_{entity}():\n    return ''.join(str(item) for item in items)"},
    {"lang": "python", "issue": "File Handle Leak",
     "bad": "def read_{entity}():\n    f = open(\"{file}\")\n    data = f.read()\n    return data",
     "good": "def read_{entity}():\n    with open(\"{file}\") as f:\n        return f.read()"},
    {"lang": "python", "issue": "No Timeout",
     "bad": "def fetch_{entity}():\n    response = requests.get(url)",
     "good": "def fetch_{entity}():\n    response = requests.get(url, timeout=10)\n    response.raise_for_status()\n    return response.json()"},
    {"lang": "python", "issue": "Bare Except",
     "bad": "def process_{entity}():\n    try:\n        result = do_something()\n    except:\n        pass",
     "good": "def process_{entity}():\n    try:\n        result = do_something()\n    except Exception as e:\n        logger.error(f\"Failed to process: {{e}}\")\n        raise"},
    {"lang": "python", "issue": "List Index Loop",
     "bad": "def process_{entity}():\n    for i in range(len(items)):\n        process(items[i])",
     "good": "def process_{entity}():\n    for item in items:\n        process(item)"},
    {"lang": "python", "issue": "Hardcoded Value",
     "bad": "def config_{entity}():\n    api_key = \"{hardcoded}\"",
     "good": "import os\ndef config_{entity}():\n    api_key = os.environ.get(\"API_KEY\", \"\")\n    if not api_key:\n        raise ValueError(\"API_KEY not set\")\n    return api_key"},
    {"lang": "python", "issue": "Deep Nesting",
     "bad": "def validate_{entity}(data):\n    if data:\n        if 'key' in data:\n            if data['key']:\n                return True\n    return False",
     "good": "def validate_{entity}(data):\n    if not data or 'key' not in data or not data['key']:\n        return False\n    return True"},
    {"lang": "python", "issue": "Redundant Comparison",
     "bad": "def check_{entity}(value):\n    if value == True:\n        return process()\n    return skip()",
     "good": "def check_{entity}(value):\n    if value:\n        return process()\n    return skip()"},
]

# Context values for injection
CONTEXTS = {
    "entity": ["user", "product", "order", "payment", "session", "token", "config", "cache", "log", "event",
               "task", "job", "queue", "worker", "service", "client", "server", "database", "model", "view"],
    "func": ["add_item", "remove_item", "update_item", "create_list", "merge_lists", "filter_items",
             "process_batch", "handle_request", "parse_data", "format_output"],
    "table": ["users", "products", "orders", "payments", "sessions", "tokens", "configs", "logs", "events", "tasks"],
    "file": ["data.json", "config.yaml", "users.csv", "items.xml", "settings.ini", "cache.db", "log.txt", "output.md"],
    "hardcoded": ["sk-1234567890", "admin:password123", "/tmp/data.txt", "localhost:8080", "root:root"],
}

def generate_variations():
    """Generate varied examples by injecting context."""
    entries = []
    
    for pattern in PATTERNS:
        lang = pattern["lang"]
        issue = pattern["issue"]
        bad_template = pattern["bad"]
        good_template = pattern["good"]
        
        # Generate variations for each entity
        for entity in CONTEXTS["entity"]:
            # Inject entity into templates
            bad = bad_template.format(
                entity=entity,
                func=CONTEXTS["func"][0],
                table=CONTEXTS["table"][0],
                file=CONTEXTS["file"][0],
                hardcoded=CONTEXTS["hardcoded"][0]
            )
            good = good_template.format(
                entity=entity,
                func=CONTEXTS["func"][0],
                table=CONTEXTS["table"][0],
                file=CONTEXTS["file"][0],
                hardcoded=CONTEXTS["hardcoded"][0]
            )
            
            entry = {
                "messages": [
                    {"role": "user", "content": f"Review this {lang} code for issues:\n\n```{lang}\n{bad}\n```"},
                    {"role": "assistant", "content": f"**Issue: {issue}**\n\nThe current code has a problem.\n\n**Improved Code:**\n```{lang}\n{good}\n```"}
                ],
                "source": "varied_generated",
                "tags": [lang, issue.lower().replace(" ", "_")]
            }
            entries.append(entry)
    
    # Shuffle
    random.shuffle(entries)
    return entries

def main():
    print("=" * 60)
    print("Dev-AI Varied Dataset Generation")
    print("=" * 60)
    
    entries = generate_variations()
    
    # Save
    import os
    os.makedirs(OUTPUT_FILE.parent, exist_ok=True)
    with open(OUTPUT_FILE, "w") as f:
        json.dump(entries, f, indent=2)
    
    print(f"Total entries: {len(entries)}")
    print(f"Saved to: {OUTPUT_FILE}")
    print("=" * 60)

if __name__ == "__main__":
    main()
