from app import create_app, db
from app.models.analytics import CompletedItem
from app.models.learning import LearningMaterial
from app.models.topics import Topic, TopicVideo
from app.models.assessment import AssessmentTemplate
from sqlalchemy import inspect

app = create_app('development')
with app.app_context():
    subject = 'Mathematics'
    user_id = 1
    portal = 'secondary'
    
    # Check if we can run the query I added
    try:
        # Mats count
        mats_count = LearningMaterial.query.filter(LearningMaterial.subject.in_([subject])).count()
        print(f"Mats count for {subject}: {mats_count}")
        
        # Vids count
        vids_count = TopicVideo.query.join(Topic).filter(Topic.subject.in_([subject])).count()
        print(f"Vids count for {subject}: {vids_count}")
        
        # Exams count
        exams_count = AssessmentTemplate.query.filter(AssessmentTemplate.subject.in_([subject])).count()
        print(f"Exams count for {subject}: {exams_count}")
        
        # Test Science Hub logic
        science_subjects = ['Science', 'Physics', 'Chemistry', 'Biology']
        sci_total = TopicVideo.query.join(Topic).filter(Topic.subject.in_(science_subjects)).count()
        print(f"Science Hub total videos: {sci_total}")
        
        print("Backend queries are VALID.")
    except Exception as e:
        print(f"QUERY ERROR: {e}")
