"""检查药品 cancer_type_tag_id 缺失情况"""
import asyncio
from sqlalchemy import select, func
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from app.config import settings
from app.models.business import DrugApproval, DrugLabel

async def main():
    engine = create_async_engine(settings.DATABASE_URL)
    async with AsyncSession(engine) as db:
        total = (await db.execute(select(func.count(DrugApproval.id)))).scalar()
        missing = (await db.execute(
            select(func.count(DrugApproval.id))
            .where(DrugApproval.cancer_type_tag_id.is_(None))
        )).scalar()
        print(f"Total drug approvals: {total}")
        print(f"Missing cancer_type_tag_id: {missing} ({missing*100//total}%)")

        r = await db.execute(
            select(DrugApproval).where(DrugApproval.drug_name.ilike("%KEYTRUDA%"))
        )
        for d in r.scalars():
            print(f"\n{d.drug_name} | {d.generic_name} | target={d.target}")
            print(f"  tag_id={d.cancer_type_tag_id}")
            print(f"  indication={d.indication[:100]}")

        r2 = await db.execute(select(func.count(DrugLabel.id)).where(DrugLabel.parsed_indications.isnot(None)))
        print(f"\nLabels with parsed_indications: {r2.scalar()}")
    await engine.dispose()

asyncio.run(main())
