#!/usr/bin/env python3
"""
Simple HTTP server to host banner.html on port 4001
"""

import http.server
import socketserver
import os
import sys

# Set the port
PORT = 4001

# Change to the frontend directory
os.chdir(os.path.dirname(os.path.abspath(__file__)))

class MyHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
    def end_headers(self):
        # Add CORS headers
        self.send_header('Access-Control-Allow-Origin', '*')
        self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
        self.send_header('Access-Control-Allow-Headers', 'Content-Type')
        super().end_headers()

    def do_GET(self):
        # Serve banner.html as the default page
        if self.path == '/':
            self.path = '/banner.html'
        return super().do_GET()

if __name__ == "__main__":
    try:
        with socketserver.TCPServer(("", PORT), MyHTTPRequestHandler) as httpd:
            print(f"🚀 Banner server running at http://localhost:{PORT}")
            print(f"📹 Serving banner.html as the default page")
            print(f"🎬 Video banner will auto-play and loop")
            print(f"⏹️  Press Ctrl+C to stop the server")
            print("-" * 50)
            httpd.serve_forever()
    except KeyboardInterrupt:
        print("\n⏹️  Server stopped by user")
        sys.exit(0)
    except OSError as e:
        if e.errno == 48:  # Address already in use
            print(f"❌ Port {PORT} is already in use. Please stop the other server first.")
            print(f"💡 Try: lsof -ti:{PORT} | xargs kill -9")
        else:
            print(f"❌ Error starting server: {e}")
        sys.exit(1)
