import asyncio
from sqlalchemy import text
from sqlalchemy.ext.asyncio import create_async_engine

DATABASE_URL = 'postgresql+asyncpg://scilit:scilit_prod_2026@postgres:5432/scilit'

async def main():
    engine = create_async_engine(DATABASE_URL)
    async with engine.connect() as conn:
        # Check cited_by_count distribution
        r = await conn.execute(text("""
            SELECT 
                CASE 
                    WHEN cited_by_count IS NULL OR cited_by_count = 0 THEN '0 or NULL'
                    WHEN cited_by_count BETWEEN 1 AND 5 THEN '1-5'
                    WHEN cited_by_count BETWEEN 6 AND 20 THEN '6-20'
                    WHEN cited_by_count BETWEEN 21 AND 100 THEN '21-100'
                    ELSE '100+'
                END as bucket,
                COUNT(*) as cnt,
                MAX(cited_by_count) as max_in_bucket
            FROM global_literature
            GROUP BY bucket
            ORDER BY MIN(COALESCE(cited_by_count, 0))
        """))
        print('=== Cited by count distribution ===')
        for row in r:
            print(f'  {row.bucket}: {row.cnt}')
        
        # Latest citation update time
        r = await conn.execute(text("""
            SELECT MAX(updated_at) as last_update, COUNT(*) as total_with_citations
            FROM global_literature WHERE cited_by_count IS NOT NULL AND cited_by_count > 0
        """))
        for row in r:
            print(f'\nLast citation update: {row.last_update}')
            print(f'Articles with citations: {row.total_with_citations}')
        
        # Check what refresh_hot_articles_cache filters by
        r = await conn.execute(text("""
            SELECT cited_by_count, pub_date, pmid FROM global_literature
            WHERE cited_by_count IS NOT NULL AND cited_by_count > 0
            AND pub_date >= CURRENT_DATE - INTERVAL '365 days'
            ORDER BY cited_by_count DESC LIMIT 10
        """))
        print('\nTop cited articles in last year:')
        for row in r:
            print(f'  PMID={row.pmid} cited={row.cited_by_count} pub_date={row.pub_date}')
        
    await engine.dispose()

asyncio.run(main())
