#!/usr/bin/env python3
"""
Example script showing how to use the new multi-offset functionality
"""
import asyncio
from login import EbayLogin

async def main():
    """Example of fetching data from multiple offsets automatically"""
    ebay_login = EbayLogin()

    try:
        print("🚀 Starting eBay research with multiple offsets...")

        # Load credentials from config file
        email, password = ebay_login.load_credentials()
        if not email or not password:
            print("Failed to load credentials from config.info file")
            return

        # Start browser
        await ebay_login.start_browser(headless=False)

        # Login once (subsequent calls will skip login check for speed)
        if await ebay_login.check_login_status():
            print("🎉 Already logged in! Using saved session.")
        else:
            print("🔐 Logging in...")
            success = await ebay_login.login_to_ebay(email, password)
            if not success:
                print("❌ Login failed!")
                return

        # Fetch data from multiple offsets automatically
        # This will fetch from offset 0, 100, 150, 200, and 250 and combine results
        combined_data = await ebay_login.fetch_multiple_offsets(
            keywords="iphone case",
            day_range=90,
            limit=50,
            offsets=[0, 100, 150, 200, 250]  # Will stop early if no results found
        )

        if combined_data:
            print(f"\n✅ Success! Collected {combined_data['total_items']} items total")
            print(f"📄 Successful fetches: {combined_data['search_parameters']['successful_fetches']}")
            print(f"💾 Data saved to files/ directory with combined results")
        else:
            print("❌ No data collected")

    except Exception as e:
        print(f"Error: {str(e)}")
    finally:
        await ebay_login.close_browser()

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