from app import app
from database.manager import DatabaseManager
from database.models import MonthlySummaryAnalysis
from sqlalchemy import func

db = DatabaseManager()
session = db.get_session()

print("Checking 2025 data...")
count = session.query(func.count(MonthlySummaryAnalysis.id)).filter(MonthlySummaryAnalysis.year == 2025).scalar()
print(f"Total rows for 2025: {count}")

print("Checking 2024 data...")
count_24 = session.query(func.count(MonthlySummaryAnalysis.id)).filter(MonthlySummaryAnalysis.year == 2024).scalar()
print(f"Total rows for 2024: {count_24}")

print("Checking Top 12 Query Logic...")
# Simulate div_dist_q logic manually
try:
    q = session.query(
        MonthlySummaryAnalysis.division,
        func.sum(MonthlySummaryAnalysis.sales_amt_curr).label('sales')
    ).group_by(MonthlySummaryAnalysis.division).order_by(func.sum(MonthlySummaryAnalysis.sales_amt_curr).desc()).limit(12)
    
    # Apply NO filters first
    print(f"Top 12 (No Filters) Count: {len(q.all())}")
    
    # Apply Month filter (e.g. month=1)
    q_month = q.filter(MonthlySummaryAnalysis.month == 1)
    print(f"Top 12 (Month=1) Count: {len(q_month.all())}")

except Exception as e:
    print(f"Query failed: {e}")
