import requests
import threading
import time
from http.server import BaseHTTPRequestHandler, HTTPServer

class LoadBalancer:
    def __init__(self, backends):
        self.backends = backends
        self.current = 0
        self.lock = threading.Lock()
        self.healthy_backends = set(backends)
        self.health_check_interval = 2  # seconds

    def health_check(self):

        while True:
            # ping the /health endpoint on each backend and
            # add or remove them from the healthy_backends object

            # TODO

            # wait this much time to reload
            time.sleep(self.health_check_interval)

    def get_backend(self):
        # implement logic to select a backend from the list of healthy_backends

        # TODO

        # please note how you're selecting the backend
        pass

    def handle_request(self, request):
        backend = self.get_backend()
        # Forward request to the backend (simplified)
        response = requests.get(f'http://{backend}{request.path}')
        return response

    # TODO add some mechanism to load a fresh set of backends every 60 sec
    def load_backends(self):
        url = "http://localhost:8080/backends"
        response = requests.get(url)
        backends = [ f"http://localhost:{backend}" for backend in response.json()]
        print(backends)
        self.backends = backends

class RequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        response = load_balancer.handle_request(self)
        self.send_response(response.status_code)
        self.end_headers()
        self.wfile.write(response.content)

def run_loadbalancer_endpoint():
    server_address = ('', 8081)
    httpd = HTTPServer(server_address, RequestHandler)
    httpd.serve_forever()

if __name__ == '__main__':

    url = "http://localhost:8080/backends"
    response = requests.get(url)
    backends = [ f"http://localhost:{backend}" for backend in response.json()]

    load_balancer = LoadBalancer(backends)

    # Start health check thread
    threading.Thread(target=load_balancer.health_check, daemon=True).start()

    # Start the server
    run_loadbalancer_endpoint()
