#!/usr/bin/env python3
"""
Test script to verify data extraction from CalendarPost and CalendarPostType.
"""

import sys
import os

# Add the project root to the Python path
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

from src.utils.db import get_db_session
from src.marketing.apps.post.model import CalendarPost, CalendarPostType


def test_data_extraction():
    """Test the data extraction logic."""
    print("🧪 Testing Data Extraction Logic")
    print("=" * 50)
    
    store_id = 35
    branch_id = 53
    
    try:
        # Get database session
        db = get_db_session()
        
        # Test the same query logic
        query = db.query(CalendarPost, CalendarPostType).join(CalendarPostType).filter(
            CalendarPost.store_id == store_id,
            CalendarPost.is_deleted == False,
            CalendarPostType.is_deleted == False
        )
        
        if branch_id:
            query = query.filter(CalendarPost.branch_id == branch_id)
        
        posts = query.limit(10).all()  # Limit to 10 posts for testing
        
        print(f"📊 Found {len(posts)} posts for store {store_id}")
        print()
        
        if not posts:
            print("❌ No posts found")
            return
        
        # Check data structure
        for i, (post, post_type) in enumerate(posts[:3]):  # Show first 3
            print(f"📝 Post {i+1}:")
            print(f"   ID: {post.id}")
            print(f"   Title: {post.title}")
            print(f"   Schedule Date: {post.schedule_date}")
            print(f"   Post Type ID: {post_type.id}")
            print(f"   Post Type: {post_type.post_type}")
            print(f"   Content: {post_type.content[:100] if post_type.content else 'No Content'}...")
            print(f"   Link: {post_type.link}")
            print()
        
        # Test content extraction
        post_content = []
        post_types = []
        
        for post, post_type in posts:
            if post_type.content:
                post_content.append(post_type.content[:200])
            
            if post_type.post_type:
                post_types.append(post_type.post_type)
        
        print(f"📋 Content Summary:")
        print(f"   Posts with content: {len(post_content)}")
        print(f"   Unique post types: {len(set(post_types))}")
        print(f"   Post types: {', '.join(set(post_types))}")
        
        if post_content:
            print(f"\n📄 Sample Content:")
            for i, content in enumerate(post_content[:2]):
                print(f"   {i+1}. {content[:100]}...")
        
        db.close()
        
    except Exception as e:
        print(f"❌ Error during testing: {str(e)}")
        import traceback
        traceback.print_exc()
        
        if 'db' in locals():
            db.close()


if __name__ == "__main__":
    print("🚀 Data Extraction Test")
    print("=" * 50)
    
    test_data_extraction()
    
    print("\n�� Test completed!")
