"""Additional integrity checks"""
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:
        # 1. Records where article_date has real month/day but pub_date is Jan-01
        r = await conn.execute(text("""
            SELECT id, pmid, pub_date, article_date FROM global_literature
            WHERE EXTRACT(MONTH FROM pub_date)=1 AND EXTRACT(DAY FROM pub_date)=1
            AND article_date IS NOT NULL 
            AND NOT (EXTRACT(MONTH FROM article_date)=1 AND EXTRACT(DAY FROM article_date)=1)
            LIMIT 20
        """))
        print('=== Records where article_date can fix pub_date ===')
        for row in r:
            print(f'  pmid={row.pmid} pub_date={row.pub_date} article_date={row.article_date}')

        # 2. Check for records with NULL pub_year but valid pub_date
        r = await conn.execute(text("""
            SELECT COUNT(*) FROM global_literature
            WHERE pub_date IS NOT NULL AND pub_year IS NULL
        """))
        print(f'\npub_date set but pub_year NULL: {r.scalar()}')

        # 3. Check for records with NULL pub_date but valid pub_year
        r = await conn.execute(text("""
            SELECT COUNT(*) FROM global_literature
            WHERE pub_date IS NULL AND pub_year IS NOT NULL
        """))
        print(f'pub_year set but pub_date NULL: {r.scalar()}')

        # 4. Check if there are records WITHOUT any date info at all
        r = await conn.execute(text("""
            SELECT COUNT(*) FROM global_literature
            WHERE pub_date IS NULL AND pub_year IS NULL AND article_date IS NULL
        """))
        print(f'No date info at all: {r.scalar()}')
        
        # 5. Latest literature records - sample top 10 for sanity check
        r = await conn.execute(text("""
            SELECT pmid, pub_date, pub_year, title FROM global_literature
            ORDER BY pub_year DESC NULLS LAST, pmid DESC LIMIT 5
        """))
        print('\n=== Latest 5 records ===')
        for row in r:
            title_short = (row.title or '')[:60]
            print(f'  PMID={row.pmid} date={row.pub_date} year={row.pub_year} title={title_short}...')
        
        # 6. Check for records that are in user_feed but patient hasn't been generated
        r = await conn.execute(text("""
            SELECT COUNT(*) FROM user_feed uf
            WHERE NOT EXISTS (SELECT 1 FROM global_literature gl WHERE gl.id = uf.literature_id)
        """))
        print(f'\nUserFeed pointing to non-existent literature: {r.scalar()}')
        
    await engine.dispose()

asyncio.run(main())
