#!/usr/bin/env python3
"""
Automated Production Backlog Capture
Runs without user interaction for automated deployment
"""

import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent))

from production_backlog_capture import ProductionBacklogCapture
import logging

# Set up logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler('automated_backlog_capture.log'),
        logging.StreamHandler()
    ]
)
logger = logging.getLogger(__name__)

def main():
    """Automated execution without user prompts"""
    logger.info("🚀 Starting automated production backlog capture")
    logger.info("=" * 60)
    logger.info("Downloading complete historical content from ALL sources")
    logger.info("Including all available media files (images, videos, audio)")
    logger.info("=" * 60)
    
    # Initialize capture
    capture = ProductionBacklogCapture()
    
    # Capture all backlogs automatically
    summary = capture.capture_all_backlogs()
    
    # Sync to NAS if any content was captured
    if summary["total_items"] > 0:
        nas_success = capture.sync_to_nas()
        summary["nas_sync_success"] = nas_success
    else:
        logger.warning("No content captured - skipping NAS sync")
        summary["nas_sync_success"] = False
    
    # Final summary
    logger.info(f"🎉 AUTOMATED BACKLOG CAPTURE COMPLETE!")
    logger.info(f"📊 Summary:")
    logger.info(f"   • Total items captured: {summary['total_items']:,}")
    logger.info(f"   • Total media files: {summary['total_media_files']:,}")
    logger.info(f"   • Sources processed: {summary['successful_sources']}/{summary['total_sources']}")
    logger.info(f"   • Duration: {summary['total_duration']/60:.1f} minutes")
    logger.info(f"   • NAS sync: {'✅' if summary.get('nas_sync_success') else '❌'}")
    
    return summary["successful_sources"] > 0

if __name__ == "__main__":
    try:
        success = main()
        sys.exit(0 if success else 1)
    except KeyboardInterrupt:
        logger.info("Automated backlog capture interrupted")
        sys.exit(1)
    except Exception as e:
        logger.critical(f"Automated backlog capture failed: {e}")
        sys.exit(2)