from pathlib import Path
import argparse
from http import server as http
import http.server

BASE_DIR = Path(__file__).resolve().parent.parent / "webui"

def main():
    parser = argparse.ArgumentParser(description="Serve Multiplayer Crossword Frontend")
    parser.add_argument("--host", type=str, default="0.0.0.0")
    parser.add_argument("--port", type=int, default=8000)
    parser.add_argument("--no-file-list", action="store_true", help="Disable directory listing")

    args = parser.parse_args()

    path = BASE_DIR
    host = args.host
    port = args.port
    no_file_list = args.no_file_list

    class CustomHandler(http.server.SimpleHTTPRequestHandler):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, directory=str(path), **kwargs)

        if no_file_list:
            def list_directory(self, path):
                self.send_error(403, "Directory listing not allowed")
                return None
    server_address = (host, port)
    httpd = http.server.HTTPServer(server_address, CustomHandler)
    print(f"Serving frontend at http://{host}:{port}/ from {path}")
    httpd.serve_forever()

if __name__ == "__main__":
    main()
