from fastapi import APIRouter, Request
from typing import Any, List, Dict, Union
from app.services.tjnetwork import (
    Any,
    ChangeSet,
    PIPE_STATUS_OPEN,
    add_pipe,
    delete_pipe,
    get_all_pipes,
    get_pipe,
    get_pipe_schema,
    set_pipe,
)

router = APIRouter()

@router.get("/getpipeschema")
async def fastapi_get_pipe_schema(network: str) -> dict[str, dict[str, Any]]:
    return get_pipe_schema(network)

@router.post("/addpipe/", response_model=None)
async def fastapi_add_pipe(
    network: str,
    pipe: str,
    node1: str,
    node2: str,
    length: float = 0,
    diameter: float = 0,
    roughness: float = 0,
    minor_loss: float = 0,
    status: str = PIPE_STATUS_OPEN,
) -> ChangeSet:
    ps = {
        "id": pipe,
        "node1": node1,
        "node2": node2,
        "length": length,
        "diameter": diameter,
        "roughness": roughness,
        "minor_loss": minor_loss,
        "status": status,
    }
    return add_pipe(network, ChangeSet(ps))

@router.post("/deletepipe/", response_model=None)
async def fastapi_delete_pipe(network: str, pipe: str) -> ChangeSet:
    ps = {"id": pipe}
    return delete_pipe(network, ChangeSet(ps))

@router.get("/getpipenode1/")
async def fastapi_get_pipe_node1(network: str, pipe: str) -> str | None:
    ps = get_pipe(network, pipe)
    return ps["node1"]

@router.get("/getpipenode2/")
async def fastapi_get_pipe_node2(network: str, pipe: str) -> str | None:
    ps = get_pipe(network, pipe)
    return ps["node2"]

@router.get("/getpipelength/")
async def fastapi_get_pipe_length(network: str, pipe: str) -> float | None:
    ps = get_pipe(network, pipe)
    return ps["length"]

@router.get("/getpipediameter/")
async def fastapi_get_pipe_diameter(network: str, pipe: str) -> float | None:
    ps = get_pipe(network, pipe)
    return ps["diameter"]

@router.get("/getpiperoughness/")
async def fastapi_get_pipe_roughness(network: str, pipe: str) -> float | None:
    ps = get_pipe(network, pipe)
    return ps["roughness"]

@router.get("/getpipeminorloss/")
async def fastapi_get_pipe_minor_loss(network: str, pipe: str) -> float | None:
    ps = get_pipe(network, pipe)
    return ps["minor_loss"]

@router.get("/getpipestatus/")
async def fastapi_get_pipe_status(network: str, pipe: str) -> str | None:
    ps = get_pipe(network, pipe)
    return ps["status"]

@router.post("/setpipenode1/", response_model=None)
async def fastapi_set_pipe_node1(network: str, pipe: str, node1: str) -> ChangeSet:
    ps = {"id": pipe, "node1": node1}
    return set_pipe(network, ChangeSet(ps))

@router.post("/setpipenode2/", response_model=None)
async def fastapi_set_pipe_node2(network: str, pipe: str, node2: str) -> ChangeSet:
    ps = {"id": pipe, "node2": node2}
    return set_pipe(network, ChangeSet(ps))

@router.post("/setpipelength/", response_model=None)
async def fastapi_set_pipe_length(network: str, pipe: str, length: float) -> ChangeSet:
    ps = {"id": pipe, "length": length}
    return set_pipe(network, ChangeSet(ps))

@router.post("/setpipediameter/", response_model=None)
async def fastapi_set_pipe_diameter(
    network: str, pipe: str, diameter: float
) -> ChangeSet:
    ps = {"id": pipe, "diameter": diameter}
    return set_pipe(network, ChangeSet(ps))

@router.post("/setpiperoughness/", response_model=None)
async def fastapi_set_pipe_roughness(
    network: str, pipe: str, roughness: float
) -> ChangeSet:
    ps = {"id": pipe, "roughness": roughness}
    return set_pipe(network, ChangeSet(ps))

@router.post("/setpipeminorloss/", response_model=None)
async def fastapi_set_pipe_minor_loss(
    network: str, pipe: str, minor_loss: float
) -> ChangeSet:
    ps = {"id": pipe, "minor_loss": minor_loss}
    return set_pipe(network, ChangeSet(ps))

@router.post("/setpipestatus/", response_model=None)
async def fastapi_set_pipe_status(network: str, pipe: str, status: str) -> ChangeSet:
    ps = {"id": pipe, "status": status}
    return set_pipe(network, ChangeSet(ps))

@router.get("/getpipeproperties/")
async def fastapi_get_pipe_properties(network: str, pipe: str) -> dict[str, Any]:
    return get_pipe(network, pipe)

@router.get("/getallpipeproperties/")
async def fastapi_get_all_pipe_properties(network: str) -> list[dict[str, Any]]:
    # 缓存查询结果提高性能
    # global redis_client
    results = get_all_pipes(network)
    return results

@router.post("/setpipeproperties/", response_model=None)
async def fastapi_set_pipe_properties(
    network: str, pipe: str, req: Request
) -> ChangeSet:
    props = await req.json()
    ps = {"id": pipe} | props
    return set_pipe(network, ChangeSet(ps))
