#!/usr/bin/env python3
"""
Main script for Image Quality Assessment using DeQA scoring.
"""

import argparse
import sys
from pathlib import Path

# Add src directory to path for imports
sys.path.append(str(Path(__file__).parent / "src"))

from src.iqa_analyzer import IQAAnalyzer
from src.logger_config import setup_logging


def main():
    """Main function to run the IQA analysis."""
    parser = argparse.ArgumentParser(
        description="IQA Benchmark Runner - Analyze image quality using DeQA scoring",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  # Run analysis on default image directory
  python main.py
  
  # Run analysis on specific directory
  python main.py --image-dir /path/to/images
  
  # Run with verbose logging and save results as CSV
  python main.py --verbose --output-format csv
  
  # Run analysis and save to custom output directory
  python main.py --output-dir custom_results
  
  # Run with memory-efficient batch processing
  python main.py --deqa-only --batch-size 4 --max-image-size 512 512
        """
    )
    
    parser.add_argument(
        '--image-dir',
        type=str,
        default='data/task/cni/images',
        help='Directory containing images to analyze (default: data/task/cni/images)'
    )
    
    parser.add_argument(
        '--output-dir',
        type=str,
        default='results',
        help='Directory to save results and reports (default: results)'
    )
    
    parser.add_argument(
        '--log-dir',
        type=str,
        default='logs',
        help='Directory to store log files (default: logs)'
    )
    
    parser.add_argument(
        '--output-format',
        choices=['json', 'csv', 'txt'],
        default='json',
        help='Output format for results (default: json)'
    )
    
    parser.add_argument(
        '--verbose', '-v',
        action='store_true',
        help='Enable verbose logging'
    )
    
    parser.add_argument(
        '--enable-deqa',
        action='store_true',
        default=True,
        help='Enable DeQA metric (default: True)'
    )
    
    parser.add_argument(
        '--enable-traditional',
        action='store_true',
        default=False,
        help='Enable traditional metrics (default: False)'
    )

    parser.add_argument(
        '--enable-pyiqa',
        action='store_true',
        default=True,
        help='Enable PyIQA metrics (default: True)'
    )

    parser.add_argument(
        '--pyiqa-top20',
        action='store_true',
        help='Use curated top 20 PyIQA metrics only'
    )
    
    parser.add_argument(
        '--deqa-only',
        action='store_true',
        help='Use only DeQA metric (disable traditional metrics)'
    )

    parser.add_argument(
        '--batch-size',
        type=int,
        default=8,
        help='Batch size for memory-efficient processing (default: 8)'
    )

    parser.add_argument(
        '--max-image-size',
        type=int,
        nargs=2,
        default=[1024, 1024],
        metavar=('WIDTH', 'HEIGHT'),
        help='Maximum image dimensions for preprocessing (default: 1024 1024)'
    )

    parser.add_argument(
        '--disable-memory-monitoring',
        action='store_true',
        help='Disable memory usage monitoring'
    )
    
    args = parser.parse_args()
    
    # Setup logging
    log_level = "DEBUG" if args.verbose else "INFO"
    logger = setup_logging(log_dir=args.log_dir, log_level=log_level)
    
    # Validate image directory
    image_dir = Path(args.image_dir)
    if not image_dir.exists():
        logger.error(f"Image directory does not exist: {image_dir}")
        sys.exit(1)
    
    if not image_dir.is_dir():
        logger.error(f"Path is not a directory: {image_dir}")
        sys.exit(1)
    
    # Initialize and run IQA analysis
    try:
        logger.info("Starting IQA analysis...")
        logger.info(f"Image directory: {image_dir}")
        logger.info(f"Output directory: {args.output_dir}")
        
        # Determine which metrics to enable
        if args.deqa_only:
            # DeQA only: disable all other metric groups explicitly
            enable_deqa = True
            enable_traditional = False
            enable_pyiqa_flag = False
        else:
            enable_deqa = args.enable_deqa
            enable_traditional = args.enable_traditional
            enable_pyiqa_flag = args.enable_pyiqa
        
        # User-requested 20 metrics (NR + FR) from PyIQA
        selected_top20 = [
            # No-Reference (for OCR practical use)
            'brisque', 'niqe', 'piqe', 'nrqm', 'nima', 'paq2piq', 'dbcnn', 'hyperiqa',
            'musiq', 'topiq_nr', 'clipiqa+_vitL14_512', 'maniqa', 'ahiq', 'unique', 'uranker',
            # Full-Reference (benchmark)
            'ssim', 'ms_ssim', 'fsim', 'vif', 'dists'
        ]

        analyzer = IQAAnalyzer(
            str(image_dir), 
            args.output_dir,
            enable_deqa=enable_deqa,
            enable_traditional=enable_traditional,
            enable_pyiqa=enable_pyiqa_flag,
            pyiqa_selected_metrics=(selected_top20 if args.pyiqa_top20 else None),
            batch_size=args.batch_size,
            max_image_size=tuple(args.max_image_size),
            enable_memory_monitoring=not args.disable_memory_monitoring
        )
        results, report = analyzer.run_analysis()
        
        # Save results only in requested format; default to TXT if none
        if args.output_format == 'csv':
            analyzer.save_results('csv')
        else:
            # TXT as the primary output for this workflow
            analyzer.save_results('txt')
        
        # Optionally display report (keep for visibility)
        print("\n" + report)
        
        logger.info("IQA analysis completed successfully!")
        
    except Exception as e:
        logger.error(f"Error during IQA analysis: {e}")
        sys.exit(1)


if __name__ == "__main__":
    main()
