"""
OmniSec CLI — Command-line interface
"""

import argparse
import json
import sys


def main():
    parser = argparse.ArgumentParser(
        prog="omnisec",
        description="OmniSec — AI · MFT · GCS · CyberSecurity · Internet CLI",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  omnisec hash sha256 "hello world"
  omnisec password generate --length 20
  omnisec password check "MyP@ssw0rd"
  omnisec port-scan 192.168.1.1 --ports 22 80 443
  omnisec ssl-info google.com
  omnisec ip-info 8.8.8.8
  omnisec dns google.com --type MX
  omnisec ping https://google.com
        """,
    )
    sub = parser.add_subparsers(dest="command")

    # Hash
    p_hash = sub.add_parser("hash", help="Hash data")
    p_hash.add_argument("algorithm", choices=["md5","sha1","sha256","sha512","sha3_256"])
    p_hash.add_argument("data", help="String to hash")

    # Password
    p_pwd = sub.add_parser("password", help="Password tools")
    pwd_sub = p_pwd.add_subparsers(dest="action")
    p_gen = pwd_sub.add_parser("generate", help="Generate a secure password")
    p_gen.add_argument("--length", type=int, default=16)
    p_gen.add_argument("--no-symbols", action="store_true")
    p_chk = pwd_sub.add_parser("check", help="Check password strength")
    p_chk.add_argument("password")

    # Port scan
    p_ps = sub.add_parser("port-scan", help="Scan TCP ports")
    p_ps.add_argument("host")
    p_ps.add_argument("--ports", nargs="+", type=int)
    p_ps.add_argument("--timeout", type=float, default=1.0)

    # SSL
    p_ssl = sub.add_parser("ssl-info", help="Inspect SSL certificate")
    p_ssl.add_argument("hostname")
    p_ssl.add_argument("--port", type=int, default=443)

    # IP info
    p_ip = sub.add_parser("ip-info", help="Get IP geolocation info")
    p_ip.add_argument("ip")

    # DNS
    p_dns = sub.add_parser("dns", help="DNS resolution")
    p_dns.add_argument("hostname")
    p_dns.add_argument("--type", default="A", dest="record_type")

    # Ping
    p_ping = sub.add_parser("ping", help="Check URL availability")
    p_ping.add_argument("url")

    # IOC extract
    p_ioc = sub.add_parser("extract-iocs", help="Extract IoCs from text")
    p_ioc.add_argument("text")

    args = parser.parse_args()

    if not args.command:
        parser.print_help()
        sys.exit(0)

    def _print(data):
        print(json.dumps(data, indent=2, default=str))

    if args.command == "hash":
        from omnisec.security import SecurityToolkit
        sec = SecurityToolkit()
        print(sec.hash(args.data, args.algorithm))

    elif args.command == "password":
        from omnisec.security import SecurityToolkit
        sec = SecurityToolkit()
        if args.action == "generate":
            print(sec.generate_password(args.length, use_symbols=not args.no_symbols))
        elif args.action == "check":
            _print(sec.check_password_strength(args.password))

    elif args.command == "port-scan":
        from omnisec.internet import InternetToolkit
        net = InternetToolkit()
        _print(net.port_scan(args.host, ports=args.ports, timeout=args.timeout))

    elif args.command == "ssl-info":
        from omnisec.internet import InternetToolkit
        net = InternetToolkit()
        _print(net.ssl_info(args.hostname, args.port))

    elif args.command == "ip-info":
        from omnisec.internet import InternetToolkit
        net = InternetToolkit()
        _print(net.ip_info(args.ip))

    elif args.command == "dns":
        from omnisec.internet import InternetToolkit
        net = InternetToolkit()
        _print(net.dns_resolve(args.hostname, args.record_type))

    elif args.command == "ping":
        from omnisec.internet import InternetToolkit
        net = InternetToolkit()
        _print(net.ping_http(args.url))

    elif args.command == "extract-iocs":
        from omnisec.security import SecurityToolkit
        sec = SecurityToolkit()
        _print(sec.extract_iocs(args.text))


if __name__ == "__main__":
    main()
