#!/usr/bin/env python3
"""
Simple web server for the Custom AI Chat UI
Serves the chat interface on port 80 (open on most VPS)
"""
import http.server
import socketserver
import os

PORT = 3000
DIRECTORY = "/opt/data"

class ChatHandler(http.server.SimpleHTTPRequestHandler):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, directory=DIRECTORY, **kwargs)
    
    def do_GET(self):
        # Redirect root and /chat to the actual file
        if self.path == '/' or self.path == '/chat':
            self.path = '/custom_chat_ui.html'
        elif self.path == '/chat.html':
            self.path = '/custom_chat_ui.html'
        return super().do_GET()
    
    def log_message(self, format, *args):
        # Print logs so we can see activity
        print(f"[{self.log_date_time_string()}] {args[0]}")

# Allow reusing the address
socketserver.TCPServer.allow_reuse_address = True

try:
    with socketserver.TCPServer(("", PORT), ChatHandler) as httpd:
        print("=" * 60)
        print("✅ CUSTOM CHAT UI SERVER STARTED!")
        print("=" * 60)
        print()
        print(f"📍 Server running on port {PORT}")
        print()
        print("🌐 Open these URLs in your browser:")
        print(f"   http://72.61.215.6:{PORT}/")
        print(f"   http://72.61.215.6:{PORT}/chat.html")
        print()
        print("📁 File being served: /opt/data/custom_chat_ui.html")
        print()
        print("Press Ctrl+C to stop the server")
        print("=" * 60)
        httpd.serve_forever()
except OSError as e:
    print(f"❌ Error: Could not start server on port {PORT}")
    print(f"   Details: {e}")
    print()
    print("Try port 8080 instead...")
