#!/usr/bin/env python3
"""
Demo Asset Tracking

This script demonstrates the asset tracking functionality by showing
how new and modified assets are detected and downloaded.
"""

import asyncio
import logging
import sys
import os
from pathlib import Path

# Add the current directory to the path so we can import modules
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from auth_manager import AuthManager
from asset_tracker import AssetTracker
from image_downloader import ImageDownloader


async def demo_asset_tracking():
    """Demonstrate asset tracking functionality."""
    print("🎯 ParentZone Asset Tracking Demo")
    print("=" * 60)

    # Configuration
    email = "tudor.sitaru@gmail.com"
    password = "mTVq8uNUvY7R39EPGVAm@"
    output_dir = "downloaded_images"

    # Setup logging
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(levelname)s - %(message)s'
    )

    try:
        print("\n🔐 Step 1: Testing Authentication")
        print("-" * 40)

        # Test authentication first
        auth_manager = AuthManager()
        success = await auth_manager.login(email, password)

        if not success:
            print("❌ Authentication failed!")
            return False

        print(f"✅ Authentication successful!")
        print(f"   User: {auth_manager.user_name}")
        print(f"   Provider: {auth_manager.provider_name}")

        print(f"\n📊 Step 2: Check Current Asset Status")
        print("-" * 40)

        # Check current asset tracker status
        tracker = AssetTracker(storage_dir=output_dir)
        stats = tracker.get_stats()

        print(f"Current local assets:")
        print(f"   Total tracked: {stats['total_tracked_assets']}")
        print(f"   Existing files: {stats['existing_files']}")
        print(f"   Missing files: {stats['missing_files']}")
        print(f"   Total size: {stats['total_size_mb']} MB")

        print(f"\n⬇️  Step 3: Download Assets (First Run)")
        print("-" * 40)
        print("This will download new assets and skip existing ones...")

        # Create downloader with asset tracking enabled
        downloader = ImageDownloader(
            api_url="https://api.parentzone.me",
            list_endpoint="/v1/media/list",
            download_endpoint="/v1/media",
            output_dir=output_dir,
            email=email,
            password=password,
            track_assets=True,
            max_concurrent=3
        )

        # First download run
        print("\n🚀 Starting download...")
        await downloader.download_all_assets()

        # Show results
        print(f"\n📈 First Run Results:")
        print(f"   Total assets found: {downloader.stats['total']}")
        print(f"   Successfully downloaded: {downloader.stats['successful']}")
        print(f"   Failed downloads: {downloader.stats['failed']}")
        print(f"   Skipped (existing): {downloader.stats['skipped']}")

        if downloader.asset_tracker:
            updated_stats = downloader.asset_tracker.get_stats()
            print(f"\n📊 Updated Asset Status:")
            print(f"   Total tracked: {updated_stats['total_tracked_assets']}")
            print(f"   Existing files: {updated_stats['existing_files']}")
            print(f"   Total size: {updated_stats['total_size_mb']} MB")

        print(f"\n🔄 Step 4: Second Run (Should Skip All)")
        print("-" * 40)
        print("Running again - should detect no new assets...")

        # Reset stats for second run
        downloader.stats = {
            'total': 0,
            'successful': 0,
            'failed': 0,
            'skipped': 0
        }

        # Second download run
        await downloader.download_all_assets()

        print(f"\n📈 Second Run Results:")
        print(f"   Assets to download: {downloader.stats['total']}")
        print(f"   New downloads: {downloader.stats['successful']}")

        if downloader.stats['total'] == 0:
            print("   ✅ Perfect! No new assets found - all are up to date!")
        else:
            print(f"   Downloaded: {downloader.stats['successful']}")
            print(f"   Failed: {downloader.stats['failed']}")

        print(f"\n🧹 Step 5: Cleanup and Final Stats")
        print("-" * 40)

        if downloader.asset_tracker:
            # Cleanup any missing files
            print("Checking for missing files...")
            downloader.asset_tracker.cleanup_missing_files()

            # Final statistics
            final_stats = downloader.asset_tracker.get_stats()
            print(f"\n📊 Final Statistics:")
            print(f"   Total tracked assets: {final_stats['total_tracked_assets']}")
            print(f"   Successful downloads: {final_stats['successful_downloads']}")
            print(f"   Failed downloads: {final_stats['failed_downloads']}")
            print(f"   Existing files: {final_stats['existing_files']}")
            print(f"   Total size: {final_stats['total_size_mb']} MB")

        print(f"\n✨ Demo completed successfully!")
        print("=" * 60)

        return True

    except Exception as e:
        print(f"❌ Demo failed with error: {e}")
        import traceback
        traceback.print_exc()
        return False


def show_usage():
    """Show usage information."""
    print("Asset Tracking Demo")
    print("=" * 30)
    print("This demo shows how the asset tracking system:")
    print("• Identifies new assets to download")
    print("• Skips already downloaded assets")
    print("• Detects modified assets")
    print("• Maintains local metadata")
    print()
    print("Usage:")
    print("  python3 demo_asset_tracking.py        # Run the demo")
    print("  python3 demo_asset_tracking.py --help # Show this help")
    print()
    print("The demo will:")
    print("1. Authenticate with ParentZone API")
    print("2. Check current local asset status")
    print("3. Download new/modified assets (first run)")
    print("4. Run again to show efficient skipping")
    print("5. Display final statistics")


async def main():
    """Main function."""
    if len(sys.argv) > 1 and sys.argv[1] in ['--help', '-h']:
        show_usage()
        return 0

    print("🚀 Starting ParentZone Asset Tracking Demo...")
    success = await demo_asset_tracking()
    return 0 if success else 1


if __name__ == "__main__":
    exit(asyncio.run(main()))
