
import pytest
from sqlmodel import Session, SQLModel, create_engine
from models import User, Review, Show, Rating
from schemas import ReviewCreate
from services.gamification import award_xp
from routers.reviews import create_review
from fastapi import HTTPException

# Mock auth
def mock_get_current_user():
    return User(id=1, email="test@test.com", hashed_password="pw", is_active=True)

# Setup in-memory DB
sqlite_file_name = "test_review_debug.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"
engine = create_engine(sqlite_url)

def test_repro_review_crash():
    SQLModel.metadata.create_all(engine)
    
    with Session(engine) as session:
        # Create dummy user and show
        user = User(email="test@test.com", hashed_password="pw")
        session.add(user)
        
        show = Show(date="2025-01-01", slug="test-show")
        session.add(show)
        session.commit()
        session.refresh(user)
        session.refresh(show)
        
        print(f"User ID: {user.id}, Show ID: {show.id}")
        
        # Payload
        review_payload = ReviewCreate(
            show_id=show.id,
            content="Test Review Content",
            blurb="Test Blurb",
            score=5.0
        )
        
        try:
            print("Attempting to create review...")
            result = create_review(
                review=review_payload,
                session=session,
                current_user=user
            )
            print("Review created successfully:", result)
        except Exception as e:
            print(f"\nCRASH DETECTED: {e}")
            import traceback
            traceback.print_exc()

if __name__ == "__main__":
    test_repro_review_crash()
