from __future__ import annotations

from sqlalchemy import select, func

from app.core.db.common import CommonSessionLocal
from app.modules.clients.association_models import ClientAssociation
from app.modules.clients.models import Client


def main():
    db = CommonSessionLocal()
    try:
        total_clients = db.execute(select(func.count()).select_from(Client)).scalar_one()
        total_assoc = db.execute(select(func.count()).select_from(ClientAssociation)).scalar_one()
        missing = db.execute(select(Client.id, Client.client_code, Client.client_name).outerjoin(ClientAssociation, ClientAssociation.client_id == Client.id).where(ClientAssociation.id.is_(None))).all()
        print(f"Total clients       : {total_clients}")
        print(f"Total associations  : {total_assoc}")
        print(f"Missing associations: {len(missing)}")
        for row in missing[:50]:
            print(f"- {row.id} | {row.client_code} | {row.client_name}")
    finally:
        db.close()


if __name__ == '__main__':
    main()
