#!/usr/bin/env python3
"""
Fetch the next 1000 Instagram posts (1001-2000) and update cumulative file.
"""

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

from src.instagram_scraper_with_images import InstagramScraperWithImages
from src.base_scraper import ScraperConfig
from src.cumulative_markdown_manager import CumulativeMarkdownManager
from datetime import datetime
import pytz
import time
import logging
import instaloader

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


def fetch_next_1000_posts():
    """Fetch Instagram posts 1001-2000 and update cumulative file."""
    
    logger.info("=" * 60)
    logger.info("INSTAGRAM NEXT 1000 POSTS (1001-2000)")
    logger.info("=" * 60)
    
    # Get Atlantic timezone timestamp
    tz = pytz.timezone('America/Halifax')
    now = datetime.now(tz)
    timestamp = now.strftime('%Y-%m-%dT%H%M%S')
    
    logger.info(f"Started at: {now.strftime('%Y-%m-%d %H:%M:%S %Z')}")
    
    # Setup config
    config = ScraperConfig(
        source_name='Instagram',
        brand_name='hkia',
        data_dir=Path('data'),
        logs_dir=Path('logs'),
        timezone='America/Halifax'
    )
    
    # Initialize scraper
    scraper = InstagramScraperWithImages(config)
    cumulative_manager = CumulativeMarkdownManager(config)
    
    logger.info("Fetching posts 1001-2000 from Instagram...")
    logger.info("This will take several hours due to rate limiting")
    
    all_items = []
    posts_to_skip = 1000  # We already have the first 1000
    max_posts = 1000  # We want the next 1000
    
    try:
        # Ensure we have a valid context
        if not scraper.loader.context:
            logger.error("Failed to initialize Instagram context")
            return False
        
        # Get profile
        profile = instaloader.Profile.from_username(scraper.loader.context, scraper.target_account)
        scraper._check_rate_limit()
        
        # Get posts
        posts = profile.get_posts()
        
        post_count = 0
        skipped = 0
        
        for post in posts:
            # Skip first 1000 posts
            if skipped < posts_to_skip:
                skipped += 1
                if skipped % 100 == 0:
                    logger.info(f"Skipping post {skipped}/{posts_to_skip}...")
                continue
            
            # Stop after next 1000
            if post_count >= max_posts:
                break
            
            try:
                # Download images for this post
                image_paths = scraper._download_post_images(post, post.shortcode)
                
                # Extract post data
                post_data = {
                    'id': post.shortcode,
                    'type': scraper._get_post_type(post),
                    'caption': post.caption if post.caption else '',
                    'author': post.owner_username,
                    'publish_date': post.date_utc.isoformat(),
                    'link': f'https://www.instagram.com/p/{post.shortcode}/',
                    'likes': post.likes,
                    'comments': post.comments,
                    'views': post.video_view_count if hasattr(post, 'video_view_count') else None,
                    'media_count': post.mediacount if hasattr(post, 'mediacount') else 1,
                    'hashtags': list(post.caption_hashtags) if post.caption else [],
                    'mentions': list(post.caption_mentions) if post.caption else [],
                    'is_video': getattr(post, 'is_video', False),
                    'local_images': image_paths
                }
                
                all_items.append(post_data)
                post_count += 1
                
                # Aggressive rate limiting
                scraper._aggressive_delay()
                scraper._check_rate_limit()
                
                # Progress updates
                if post_count % 10 == 0:
                    logger.info(f"Fetched post {posts_to_skip + post_count} (#{post_count}/1000 in this batch)")
                
                # Save incremental updates every 100 posts
                if post_count % 100 == 0:
                    logger.info(f"Saving incremental update at {post_count} posts...")
                    output_file = cumulative_manager.update_cumulative_file(all_items, 'Instagram')
                    logger.info(f"Saved to: {output_file}")
                    
            except Exception as e:
                logger.error(f"Error processing post: {e}")
                continue
        
        # Final save
        if all_items:
            output_file = cumulative_manager.update_cumulative_file(all_items, 'Instagram')
            
            # Calculate statistics
            img_count = sum(len(item.get('local_images', [])) for item in all_items)
            
            logger.info("=" * 60)
            logger.info("INSTAGRAM NEXT 1000 COMPLETED")
            logger.info("=" * 60)
            logger.info(f"Posts fetched: {len(all_items)}")
            logger.info(f"Post range: 1001-{1000 + len(all_items)}")
            logger.info(f"Images downloaded: {img_count}")
            logger.info(f"Output file: {output_file}")
            logger.info("=" * 60)
            
            return True
        else:
            logger.warning("No posts fetched")
            return False
            
    except Exception as e:
        logger.error(f"Fatal error: {e}")
        import traceback
        traceback.print_exc()
        return False


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