import pandas as pd

from well_routing.classes import Graph
from commonlib_reader import Facility

for fac in Facility.get_all_facilities():
    if len(fac.ioc_plant) == 0:
        continue

    print(fac.ioc_plant)
    start_time = "01-Nov-2025 13:00"
    stop_time = "01-Feb-2026 14:00"
    chosen_time = "02-Dec-2025 12:00"
    try:
        g = Graph.get_asset_graph(fac=fac, 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 fac.identity == "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.to_csv(f"{fac.identity}_routing_table.csv")
        g.to_csv(f"{fac.identity}_routing_table_{chosen_time.replace(":","_")}.csv",timestamp=pd.to_datetime(chosen_time))
        g.to_csv(f"{fac.identity}_routing_table_{chosen_time.replace(":","_")}_no_tag.csv",tag_numbers=False, timestamp=pd.to_datetime(chosen_time))

        g.plot(timestamp=chosen_time)
    except Exception as ex:
        print(ex)
        continue

    print(f"Facility {fac} 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"{fac}_dummy_directed.xml")
    # g.save_cytoscape_json(f"{fac}_cyto_graph_2.txt")
