import sys
import os
sys.path.append('/app')
sys.path.append('/home/josmar/dev/anexo76/backend')

from sqlalchemy import create_engine, text
from sqlalchemy.orm import sessionmaker

def test():
    db_url = "postgresql://postgres:postgres@anexo76-postgres-a76:5432/anexo76_core"
    engine = create_engine(db_url)
    Session = sessionmaker(bind=engine)
    db = Session()

    try:
        print("Checking users and tenants...")
        # Check users
        users = db.execute(text("SELECT id, email, tenant_id FROM a76.users")).fetchall()
        for u in users:
            print(f"User ID: {u.id} | Email: {u.email} | Tenant ID: {u.tenant_id}")
            
        # Check companies
        companies = db.execute(text("SELECT id, name, tenant_id FROM a76.companies")).fetchall()
        for c in companies:
            print(f"Company ID: {c.id} | Name: {c.name} | Tenant ID: {c.tenant_id}")
            
        # Check fractions
        fractions = db.execute(text("SELECT id, code, tenant_id, company_id FROM a76.us_tariff_fractions")).fetchall()
        print(f"Total US Fractions in DB: {len(fractions)}")
        for f in fractions:
            print(f"Fraction ID: {f.id} | Code: {f.code} | Tenant ID: {f.tenant_id} | Company ID: {f.company_id}")

    except Exception as e:
        print(f"Error: {e}")
    finally:
        db.close()

if __name__ == "__main__":
    test()
