import asyncio
from sqlalchemy import text
from app.db import async_session


async def main():
    async with async_session() as db:
        total = await db.scalar(text("SELECT count(*) FROM global_literature"))
        wa = await db.scalar(
            text(
                "SELECT count(*) FROM global_literature WHERE abstract IS NOT NULL AND abstract != ''"
            )
        )
        ft = await db.scalar(
            text(
                "SELECT count(*) FROM global_literature WHERE full_text_sections IS NOT NULL"
            )
        )
        row = await (
            await db.execute(
                text(
                    "SELECT min(pub_date)::text, max(pub_date)::text FROM global_literature"
                )
            )
        ).fetchone()
        min_date, max_date = row
        year_only = await db.scalar(
            text(
                "SELECT count(*) FROM global_literature WHERE pub_date IS NOT NULL AND LENGTH(pub_date::text)=4"
            )
        )
        print(f"Total: {total}")
        print(f"With abstract: {wa}")
        print(f"With full text: {ft}")
        print(f"Date range: {min_date} ~ {max_date}")
        print(f"Year-only dates: {year_only}")


asyncio.run(main())
