#!/usr/bin/env python3
"""
Test ImgInn with headless Chromium (no display)
"""

import sys
import os
from pathlib import Path
from datetime import datetime

# Add modules directory to path
sys.path.insert(0, str(Path(__file__).parent))

from modules.imginn_module import ImgInnDownloader

def main():
    print("=" * 60)
    print("ImgInn Headless Test - Chromium")
    print("=" * 60)
    print(f"Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
    print("=" * 60)
    print()
    print("Testing with HEADLESS Chromium (no display needed)")
    print()

    # Get username from command line or use default
    if len(sys.argv) > 1:
        username = sys.argv[1]
    else:
        username = "evalongoria"

    print(f"Testing with username: {username}")
    print()

    # Create downloader with HEADLESS browser
    downloader = ImgInnDownloader(
        api_key="cf57fdb7577ada64d150431d6589c8f4",
        headless=True,  # HEADLESS MODE
        show_progress=True,
        use_database=False
    )

    print(f"Starting headless download test for @{username}...")
    print()

    try:
        # Try to download just 1 post to test
        files = downloader.download_posts(
            username=username,
            days_back=14,
            max_posts=1
        )

        print("\n" + "=" * 60)
        print("RESULTS")
        print("=" * 60)

        if files:
            print(f"✅ Successfully downloaded {len(files)} file(s)")
            for f in files:
                print(f"  - {Path(f).name}")
            print("\n✅ Headless Chromium works!")
        else:
            print("⚠️  No files downloaded")
            print("This might be normal if posts were already downloaded")

    except KeyboardInterrupt:
        print("\n\n⚠️  Interrupted by user")
    except Exception as e:
        print(f"\n\n❌ Error: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    main()
