import requests, urllib3, os, json
try:
    import msgpack
except ImportError:
    pass
urllib3.disable_warnings()

API_URL = "https://mtr.ciapongi.szablix.pl/mtr/api/map/stations-and-routes"
ROUTES_DIR = "/var/lib/pterodactyl/volumes/cc7ccc03-03bd-4bde-8488-3057d3803420/world/mtr/minecraft/overworld/routes"

def main():
    print("=== SZUKANIE POWIĄZAŃ PERONÓW Z TRASAMI ===")
    
    # 1. Sprawdzamy API
    print("\n[1] Sprawdzam strukturę tras w API...")
    try:
        r = requests.get(API_URL, verify=False, timeout=10)
        routes = r.json().get('data', {}).get('routes', [])
        if routes:
            # Pokażmy tylko pierwszą trasę bez całego gigantycznego 'path'
            sample_route = routes[0].copy()
            sample_route.pop('path', None) 
            print(json.dumps(sample_route, indent=4, ensure_ascii=False))
        else:
            print("Brak tras w API.")
    except Exception as e:
        print(f"Błąd API: {e}")

    # 2. Sprawdzamy pliki MsgPack
    print("\n[2] Sprawdzam folder z plikami zapisu tras (MsgPack)...")
    if os.path.exists(ROUTES_DIR):
        found = False
        for root, dirs, files in os.walk(ROUTES_DIR):
            for file in files:
                if not file.startswith("."):
                    sample_file = os.path.join(root, file)
                    try:
                        with open(sample_file, "rb") as f:
                            data = msgpack.unpackb(f.read(), raw=False)
                            print(json.dumps(data, indent=4, ensure_ascii=False))
                            found = True
                            break
                    except Exception as e:
                        print(f"Błąd dekodowania MsgPack: {e}")
            if found: break
        if not found:
            print("Nie znaleziono czytelnych plików tras na dysku.")
    else:
        print(f"Folder nie istnieje: {ROUTES_DIR}")

if __name__ == "__main__":
    main()
