#!/usr/bin/env python3
"""Test TikTok scraper with Scrapling/Camofaux."""

import sys
from pathlib import Path
from dotenv import load_dotenv
from src.tiktok_scraper_scrapling import TikTokScraperScrapling
from src.base_scraper import ScraperConfig

# Load environment variables
load_dotenv()

def test_tiktok_scraper():
    """Test TikTok scraper with real data."""
    print("\n" + "="*60)
    print("Testing TikTok Scraper with Scrapling/Camofaux")
    print("="*60)
    
    # Configure scraper
    config = ScraperConfig(
        source_name="tiktok",
        brand_name="hvacknowitall",
        data_dir=Path("test_data"),
        logs_dir=Path("logs"),
        timezone="America/Halifax"
    )
    
    # Create scraper instance
    scraper = TikTokScraperScrapling(config)
    
    try:
        # Fetch posts
        print(f"\nFetching posts from @{scraper.target_username}...")
        posts = scraper.fetch_posts(max_posts=3)
        
        if posts:
            print(f"\n✓ Successfully fetched {len(posts)} posts")
            
            # Display first post
            if posts:
                first_post = posts[0]
                print("\nFirst post details:")
                print(f"  ID: {first_post.get('id')}")
                print(f"  Link: {first_post.get('link')}")
                print(f"  Views: {first_post.get('views', 0):,}")
                caption = first_post.get('caption', '')
                if caption:
                    print(f"  Caption: {caption[:100]}...")
            
            # Generate markdown
            markdown = scraper.format_markdown(posts)
            
            # Save to file
            output_file = config.data_dir / "tiktok_test.md"
            output_file.parent.mkdir(parents=True, exist_ok=True)
            output_file.write_text(markdown)
            
            print(f"\n✓ Markdown saved to: {output_file}")
            
            # Show snippet of markdown
            lines = markdown.split('\n')[:20]
            print("\nMarkdown preview:")
            print("-" * 40)
            for line in lines:
                print(line)
            print("-" * 40)
            
        else:
            print("\n✗ No posts fetched - possible bot detection or rate limiting")
            
    except Exception as e:
        print(f"\n✗ Error: {e}")
        import traceback
        traceback.print_exc()
        return False
    
    return len(posts) > 0

if __name__ == "__main__":
    success = test_tiktok_scraper()
    sys.exit(0 if success else 1)