"""
Test that admins can access shot details
"""
import sys
from sqlalchemy.orm import Session
from database import SessionLocal, engine
from models.user import User, UserRole
from models.shot import Shot
from models.episode import Episode
from routers.shots import check_episode_access

def test_admin_access():
    """Test that admin users can access any episode"""
    db: Session = SessionLocal()
    
    try:
        # Get first episode
        episode = db.query(Episode).first()
        if not episode:
            print("No episodes found in database")
            return
        
        print(f"Testing with Episode ID: {episode.id}, Project ID: {episode.project_id}")
        
        # Get an admin user
        admin = db.query(User).filter(User.is_admin == True).first()
        if not admin:
            print("No admin user found")
            return
        
        print(f"Testing with Admin: {admin.email}, Role: {admin.role}, is_admin: {admin.is_admin}")
        
        # Test access
        try:
            result = check_episode_access(episode.id, admin, db)
            print(f"✓ SUCCESS: Admin can access episode {episode.id}")
            print(f"  Episode name: {result.name if hasattr(result, 'name') else 'N/A'}")
        except Exception as e:
            print(f"✗ FAILED: {str(e)}")
            
        # Test with coordinator
        coordinator = db.query(User).filter(User.role == UserRole.COORDINATOR).first()
        if coordinator:
            print(f"\nTesting with Coordinator: {coordinator.email}")
            try:
                result = check_episode_access(episode.id, coordinator, db)
                print(f"✓ SUCCESS: Coordinator can access episode {episode.id}")
            except Exception as e:
                print(f"✗ FAILED: {str(e)}")
        
        # Test with artist (should fail if not project member)
        artist = db.query(User).filter(User.role == UserRole.ARTIST).first()
        if artist:
            print(f"\nTesting with Artist: {artist.email}")
            try:
                result = check_episode_access(episode.id, artist, db)
                print(f"✓ Artist can access episode {episode.id} (is project member)")
            except Exception as e:
                print(f"✓ Expected behavior: {str(e)}")
                
    finally:
        db.close()

if __name__ == "__main__":
    test_admin_access()
