"""Fix pub_date from article_date where article_date has real month/day"""
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:
        # Preview
        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)
        """))
        rows = r.all()
        print(f'Found {len(rows)} records to fix via article_date:')
        for row in rows[:10]:
            print(f'  pmid={row.pmid} {row.pub_date} -> {row.article_date}')
        
        # Fix
        fixed = 0
        for row in rows:
            await conn.execute(
                text("UPDATE global_literature SET pub_date = :ad, updated_at = NOW() WHERE id = :id"),
                {"ad": row.article_date, "id": row.id},
            )
            fixed += 1
        await conn.commit()
        print(f'\nFixed {fixed} records')
        
    await engine.dispose()

asyncio.run(main())
