"""
Script Generator Tool - Premium Feature
Pre-built pentesting script templates with AI customization.
"""

from typing import Dict, List

# Script Templates
TEMPLATES = {
    "recon": {
        "name": "🔍 Reconocimiento Básico",
        "description": "Nmap + Whois + DNS lookup",
        "script": '''#!/bin/bash
# Reconocimiento Básico - Generated by DOMINION
# Target: {target}

echo "[*] Starting reconnaissance on {target}"

echo "[+] Whois lookup..."
whois {target} | head -30

echo "[+] DNS enumeration..."
dig {target} ANY +short

echo "[+] Nmap quick scan..."
nmap -sV -sC -T4 {target} -oN recon_{target}.txt

echo "[*] Recon complete! Results saved to recon_{target}.txt"
'''
    },
    "web": {
        "name": "🌐 Web Scanner",
        "description": "Nikto + Gobuster + Headers",
        "script": '''#!/bin/bash
# Web Scanner - Generated by DOMINION
# Target: {target}

echo "[*] Web scanning {target}"

echo "[+] HTTP Headers..."
curl -I {target} 2>/dev/null | head -20

echo "[+] Nikto scan..."
nikto -h {target} -o nikto_{target}.txt

echo "[+] Directory bruteforce..."
gobuster dir -u {target} -w /usr/share/wordlists/dirb/common.txt -o dirs_{target}.txt

echo "[*] Web scan complete!"
'''
    },
    "vuln": {
        "name": "⚠️ Vulnerability Scan",
        "description": "Nmap scripts + Searchsploit",
        "script": '''#!/bin/bash
# Vulnerability Scan - Generated by DOMINION
# Target: {target}

echo "[*] Vulnerability scanning {target}"

echo "[+] Nmap vuln scripts..."
nmap --script vuln -T4 {target} -oN vuln_{target}.txt

echo "[+] Searching exploits..."
searchsploit {target} | head -20

echo "[*] Vuln scan complete!"
'''
    },
    "bruteforce": {
        "name": "🔑 Bruteforce SSH",
        "description": "Hydra SSH attack template",
        "script": '''#!/bin/bash
# SSH Bruteforce - Generated by DOMINION
# Target: {target}
# DISCLAIMER: Use only on authorized systems!

echo "[*] SSH Bruteforce on {target}"
echo "[!] WARNING: Ensure you have authorization!"

# hydra -l {user} -P /usr/share/wordlists/rockyou.txt ssh://{target}

echo "[*] Template ready. Uncomment hydra line to execute."
'''
    }
}


def list_templates() -> List[Dict]:
    """Return list of available templates."""
    return [
        {"id": k, "name": v["name"], "description": v["description"]}
        for k, v in TEMPLATES.items()
    ]


def generate_script(template_id: str, target: str, **kwargs) -> str:
    """Generate a script from template with target substitution."""
    if template_id not in TEMPLATES:
        return None
    
    template = TEMPLATES[template_id]["script"]
    return template.format(target=target, **kwargs)


def save_script(content: str, filename: str) -> str:
    """Save script to file and make executable."""
    with open(filename, "w") as f:
        f.write(content)
    
    import os
    os.chmod(filename, 0o755)
    return filename
