from fastapi import APIRouter, Request
from typing import Any, List, Dict, Union
from app.services.tjnetwork import (
    Any,
    ChangeSet,
    VALVES_TYPE_PRV,
    add_valve,
    delete_valve,
    get_all_valves,
    get_valve,
    get_valve_schema,
    set_valve,
)

router = APIRouter()

@router.get("/getvalveschema")
async def fastapi_get_valve_schema(network: str) -> dict[str, dict[str, Any]]:
    return get_valve_schema(network)

@router.post("/addvalve/", response_model=None)
async def fastapi_add_valve(
    network: str,
    valve: str,
    node1: str,
    node2: str,
    diameter: float = 0,
    v_type: str = VALVES_TYPE_PRV,
    setting: float = 0,
    minor_loss: float = 0,
) -> ChangeSet:
    ps = {
        "id": valve,
        "node1": node1,
        "node2": node2,
        "diameter": diameter,
        "v_type": v_type,
        "setting": setting,
        "minor_loss": minor_loss,
    }

    return add_valve(network, ChangeSet(ps))

@router.post("/deletevalve/", response_model=None)
async def fastapi_delete_valve(network: str, valve: str) -> ChangeSet:
    ps = {"id": valve}
    return delete_valve(network, ChangeSet(ps))

@router.get("/getvalvenode1/")
async def fastapi_get_valve_node1(network: str, valve: str) -> str | None:
    ps = get_valve(network, valve)
    return ps["node1"]

@router.get("/getvalvenode2/")
async def fastapi_get_valve_node2(network: str, valve: str) -> str | None:
    ps = get_valve(network, valve)
    return ps["node2"]

@router.get("/getvalvediameter/")
async def fastapi_get_valve_diameter(network: str, valve: str) -> float | None:
    ps = get_valve(network, valve)
    return ps["diameter"]

@router.get("/getvalvetype/")
async def fastapi_get_valve_type(network: str, valve: str) -> str | None:
    ps = get_valve(network, valve)
    return ps["type"]

@router.get("/getvalvesetting/")
async def fastapi_get_valve_setting(network: str, valve: str) -> float | None:
    ps = get_valve(network, valve)
    return ps["setting"]

@router.get("/getvalveminorloss/")
async def fastapi_get_valve_minor_loss(network: str, valve: str) -> float | None:
    ps = get_valve(network, valve)
    return ps["minor_loss"]

@router.post("/setvalvenode1/", response_model=None)
async def fastapi_set_valve_node1(network: str, valve: str, node1: str) -> ChangeSet:
    ps = {"id": valve, "node1": node1}
    return set_valve(network, ChangeSet(ps))

@router.post("/setvalvenode2/", response_model=None)
async def fastapi_set_valve_node2(network: str, valve: str, node2: str) -> ChangeSet:
    ps = {"id": valve, "node2": node2}
    return set_valve(network, ChangeSet(ps))

@router.post("/setvalvenodediameter/", response_model=None)
async def fastapi_set_valve_diameter(
    network: str, valve: str, diameter: float
) -> ChangeSet:
    ps = {"id": valve, "diameter": diameter}
    return set_valve(network, ChangeSet(ps))

@router.post("/setvalvetype/", response_model=None)
async def fastapi_set_valve_type(network: str, valve: str, type: str) -> ChangeSet:
    ps = {"id": valve, "type": type}
    return set_valve(network, ChangeSet(ps))

@router.post("/setvalvesetting/", response_model=None)
async def fastapi_set_valve_setting(
    network: str, valve: str, setting: float
) -> ChangeSet:
    ps = {"id": valve, "setting": setting}
    return set_valve(network, ChangeSet(ps))

@router.get("/getvalveproperties/")
async def fastapi_get_valve_properties(network: str, valve: str) -> dict[str, Any]:
    return get_valve(network, valve)

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

@router.post("/setvalveproperties/", response_model=None)
async def fastapi_set_valve_properties(
    network: str, valve: str, req: Request
) -> ChangeSet:
    props = await req.json()
    ps = {"id": valve} | props
    return set_valve(network, ChangeSet(ps))
