import requests


def delete_queues_with_prefix_http(
    host="localhost",
    port=15672,
    username="springuser",
    password="TheCleverWho",
    prefix="cm-",
):
    """
    CLI utility. Delete queues using RabbitMQ HTTP API.
    """
    base_url = f"http://{host}:{port}/api/queues/%2F"

    try:
        response = requests.get(base_url, auth=(username, password))
        response.raise_for_status()

        queues = [q["name"] for q in response.json() if q["name"].startswith(prefix)]

        if not queues:
            print(f"No queues found with prefix '{prefix}'")
            return

        print(f"Found {len(queues)} queues with prefix '{prefix}':")
        for queue in queues:
            print(f" - {queue}")

        for queue in queues:
            try:
                delete_url = f"{base_url}/{queue}"
                response = requests.delete(delete_url, auth=(username, password))
                response.raise_for_status()
                print(f"Successfully deleted queue: {queue}")
            except Exception as e:
                print(f"Failed to delete queue {queue}: {str(e)}")

        print("Queue deletion process completed.")

    except Exception as e:
        print(f"An error occurred: {str(e)}")


if __name__ == "__main__":
    delete_queues_with_prefix_http()
    delete_queues_with_prefix_http(prefix="amq.gen--")
    delete_queues_with_prefix_http(prefix="amq_")
