"""Test that setting env vars before importing main.py fixes the crash."""
import os
import sys

# This is the KEY fix: set Windows env vars BEFORE main.py is imported
os.environ['SDL_VIDEODRIVER'] = 'windows'
os.environ['SDL_AUDIODRIVER'] = 'directsound'
os.environ['KIVY_WINDOW'] = 'sdl2'
# Use 'angle_sdl2' on Windows for better DirectX compatibility
os.environ['KIVY_GL_BACKEND'] = 'angle_sdl2'
# Let Kivy auto-detect input providers on Windows
os.environ['KIVY_INPUTPROVIDERS'] = ''
os.environ['KIVY_VIDEO'] = 'ffpyplayer'
os.environ['KIVY_AUDIO'] = 'ffpyplayer'
os.environ['FFPYPLAYER_CODECS'] = 'h264,h265,vp9,vp8'
os.environ['SDL_VIDEO_ALLOW_SCREENSAVER'] = '0'

# Add src to path
sys.path.insert(0, r'C:\Users\Dell-PC\Desktop\Kiwy-Signage\src')

print("=" * 60)
print("Testing main.py import with Windows env vars...")
print("=" * 60)

try:
    import main
    print("SUCCESS: main.py imported without crashing!")
    print(f"  SDL_VIDEODRIVER = {os.environ.get('SDL_VIDEODRIVER')}")
    print(f"  KIVY_WINDOW = {os.environ.get('KIVY_WINDOW')}")
    print(f"  KIVY_GL_BACKEND = {os.environ.get('KIVY_GL_BACKEND')}")
    print(f"  KIVY_INPUTPROVIDERS = {os.environ.get('KIVY_INPUTPROVIDERS')}")
except SystemExit as e:
    print(f"FAILED: SystemExit({e}) - Kivy window provider still not loading")
    sys.exit(1)
except Exception as e:
    print(f"FAILED with exception: {e}")
    import traceback
    traceback.print_exc()
    sys.exit(1)
