import networkx as nx
import pandas as pd

from well_routing.classes import Graph
from commonlib_reader import Facility

for asset in [x.inst_code for x in Facility.get_all_facilities()]:
    if len(asset) > 0:
        print(asset)
    start_time = "01-Dec-2025 13:00"
    stop_time = "05-Dec-2025 14:00"
    chosen_time = "02-Dec-2025 12:00"
    try:
        g = Graph.get_asset_graph(
            stid_code=asset, start_time=start_time, stop_time=stop_time
        )
        if len(g.nodes) == 0:
            print("No graph data found")
            continue
    except Exception as ex:
        print(ex)
        continue
    if asset == "ASGA":
        df = g.get_minimal_routing_table(
            g.get_nodes(node_type="well"),
            ["ASGA_TEST_MAN", "ASGA_PROD_MAN_B"],
            timestamp=pd.to_datetime(chosen_time),
        )

    try:
        G = g.convert_to_networkx(timestamp=pd.to_datetime(chosen_time))
        g.plot(timestamp=chosen_time)
    except Exception as ex:
        print(ex)
        continue

    print(f"Asset {asset} has a graph with time series data.")

    # path = g.shortest_path(
    #     "NO 34/7-P-43",
    #     "SNA-PROD-INLET-20BVA01",
    #     timestamp=pd.to_datetime("01-Dec-2025 12:00"),
    # )
    # length = g.path_weight(path, timestamp=pd.to_datetime("01-Dec-2025 12:00"))

    # path = g.shortest_path(
    #     "NO 34/7-A-16",
    #     "SNA-TEST-INLET-20AVA01",
    #     timestamp=pd.to_datetime("01-Dec-2025 12:00"),
    # )
    # length = g.path_weight(path, timestamp=pd.to_datetime("01-Dec-2025 12:00"))

    nx.write_graphml(G, f"{asset}_dummy_directed.xml")
    g.save_cytoscape_json(f"{asset}_cyto_graph_2.txt")
