#!/usr/bin/env python3
"""
Manual script to refresh the puja API cache.

Usage:
    python scripts/refresh_puja_cache.py

This script will:
1. Fetch fresh data from the Karishye backend API
2. Save it to the cache file
3. Display a summary of the cached data
"""
import asyncio
import logging
import sys
from pathlib import Path

# Add parent directory to path so we can import from app
sys.path.insert(0, str(Path(__file__).parent.parent))

from app.logging_config import setup_logging
from app.utils.puja_api_cache import refresh_cache, get_cache_instance

# Setup colored logging with timestamps
setup_logging(
    level=logging.INFO,
    use_colors=True
)
logger = logging.getLogger(__name__)


async def main():
    """Main function to refresh the cache."""
    logger.info("KARISHYE PUJA API CACHE REFRESH")
    
    # Refresh cache
    logger.info("Fetching fresh data from API...")
    success = await refresh_cache()
    
    if success:
        logger.info("✓ Cache refreshed successfully")
        
        # Display summary
        cache = get_cache_instance()
        data = await cache.get_puja_data()
        
        if data:
            # Display summary based on data structure
            if isinstance(data, list):
                logger.info(f"Total pujas cached: {len(data)}")
            elif isinstance(data, dict):
                if 'pujas' in data:
                    logger.info(f"Total pujas cached: {len(data['pujas'])}")
                else:
                    logger.info(f"Cache contains {len(data.keys())} keys")
            
            logger.info("✓ Cache is ready for use")
        else:
            logger.error("Failed to load cached data")
            
    else:
        logger.error("Failed to refresh cache - check your internet connection and API endpoint")


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