#!/usr/bin/env python3
"""
rfind - Command line interface
"""

import sys
import argparse
from . import scan_directory, smart_scan, parallel_scan

def main():
    parser = argparse.ArgumentParser(
        prog="rfind",
        description="Discover files when you have execute but not read permission",
        epilog="Examples:\n  rfind /system/bin\n  rfind -M 4 -c 'a-z0-9' /dev\n  rfind --smart /d"
    )
    
    parser.add_argument("path", nargs="?", default=".", 
                       help="target directory (default: current)")
    parser.add_argument("-m", "--min", type=int, default=1,
                       help="minimum filename length (default: 1)")
    parser.add_argument("-M", "--max", type=int, default=3,
                       help="maximum filename length (default: 3)")
    parser.add_argument("-c", "--chars", default="abcdefghijklmnopqrstuvwxyz",
                       help="character set to use (default: a-z)")
    parser.add_argument("-t", "--threads", type=int, default=1,
                       help="number of threads (default: 1)")
    parser.add_argument("-s", "--smart", action="store_true",
                       help="use smart pattern scanning")
    parser.add_argument("-v", "--verbose", action="store_true",
                       help="verbose output")
    parser.add_argument("--version", action="store_true",
                       help="show version and exit")
    
    args = parser.parse_args()
    
    if args.version:
        from . import __version__
        print(f"rfind {__version__}")
        return 0
    
    if args.verbose:
        print(f"Scanning: {args.path}")
        print(f"Length: {args.min}-{args.max}")
        print(f"Charset: {args.chars}")
        print(f"Threads: {args.threads}")
        print(f"Mode: {'Smart' if args.smart else 'Brute-force'}")
        print("-" * 40)
    
    if args.smart:
        results = smart_scan(args.path, args.chars, args.min, args.max)
    elif args.threads > 1:
        results = parallel_scan(args.path, args.chars, args.min, args.max, args.threads)
    else:
        results = scan_directory(args.path, args.chars, args.min, args.max, args.threads)
    
    if not results:
        print("No files found.")
        return 0
    
    # Group by type
    files = []
    dirs = []
    
    for name, ftype in results:
        if ftype == 'file':
            files.append(name)
        else:
            dirs.append(name)
    
    if dirs:
        print("\nDIRECTORIES:")
        for name in sorted(dirs):
            print(f"  {name}")
    
    if files:
        print("\nFILES:")
        for name in sorted(files):
            print(f"  {name}")
    
    if args.verbose:
        print(f"\nSummary: {len(dirs)} directories, {len(files)} files")
    
    return 0

if __name__ == "__main__":
    sys.exit(main())
