#!/usr/bin/env python3
"""Generate booking QR code at container startup"""
import os
import sys
import subprocess

def generate_booking_qr():
    """Generate QR code for the booking page"""
    # Get base URL from environment, default to localhost
    base_url = os.environ.get('BASE_URL', 'http://localhost')
    booking_url = f"{base_url}/book"
    
    # Create output directory if it doesn't exist
    output_dir = '/web/assets'
    os.makedirs(output_dir, exist_ok=True)
    
    output_file = f'{output_dir}/booking-qr.png'
    
    try:
        # Generate QR code using qrencode
        subprocess.run(
            ['qrencode', '-o', output_file, '-s', '5', booking_url],
            check=True,
            capture_output=True
        )
        print(f"✓ Generated booking QR code: {output_file}")
        print(f"  URL: {booking_url}")
        return True
    except subprocess.CalledProcessError as e:
        print(f"✗ Failed to generate QR code: {e.stderr.decode()}", file=sys.stderr)
        return False
    except FileNotFoundError:
        print("✗ qrencode not found. Install with: apt-get install qrencode", file=sys.stderr)
        return False

if __name__ == '__main__':
    success = generate_booking_qr()
    sys.exit(0 if success else 1)
