from fastapi import APIRouter, Request
from typing import Any, List, Dict, Union
from app.services.tjnetwork import (
    Any,
    ChangeSet,
    add_reservoir,
    delete_reservoir,
    get_all_reservoirs,
    get_reservoir,
    get_reservoir_schema,
    set_reservoir,
)

router = APIRouter()

@router.get("/getreservoirschema")
async def fast_get_reservoir_schema(network: str) -> dict[str, dict[str, Any]]:
    return get_reservoir_schema(network)

@router.post("/addreservoir/", response_model=None)
async def fastapi_add_reservoir(
    network: str, reservoir: str, x: float, y: float, head: float
) -> ChangeSet:
    ps = {"id": reservoir, "x": x, "y": y, "head": head}
    return add_reservoir(network, ChangeSet(ps))

@router.post("/deletereservoir/", response_model=None)
async def fastapi_delete_reservoir(network: str, reservoir: str) -> ChangeSet:
    ps = {"id": reservoir}
    return delete_reservoir(network, ChangeSet(ps))

@router.get("/getreservoirhead/")
async def fastapi_get_reservoir_head(network: str, reservoir: str) -> float | None:
    ps = get_reservoir(network, reservoir)
    return ps["head"]

@router.get("/getreservoirpattern/")
async def fastapi_get_reservoir_pattern(network: str, reservoir: str) -> str | None:
    ps = get_reservoir(network, reservoir)
    return ps["pattern"]

@router.get("/getreservoirx/")
async def fastapi_get_reservoir_x(
    network: str, reservoir: str
) -> dict[str, float] | None:
    ps = get_reservoir(network, reservoir)
    return ps["x"]

@router.get("/getreservoiry/")
async def fastapi_get_reservoir_y(
    network: str, reservoir: str
) -> dict[str, float] | None:
    ps = get_reservoir(network, reservoir)
    return ps["y"]

@router.get("/getreservoircoord/")
async def fastapi_get_reservoir_coord(
    network: str, reservoir: str
) -> dict[str, float] | None:
    ps = get_reservoir(network, reservoir)
    coord = {"id": reservoir, "x": ps["x"], "y": ps["y"]}
    return coord

@router.post("/setreservoirhead/", response_model=None)
async def fastapi_set_reservoir_head(
    network: str, reservoir: str, head: float
) -> ChangeSet:
    ps = {"id": reservoir, "head": head}
    return set_reservoir(network, ChangeSet(ps))

@router.post("/setreservoirpattern/", response_model=None)
async def fastapi_set_reservoir_pattern(
    network: str, reservoir: str, pattern: str
) -> ChangeSet:
    ps = {"id": reservoir, "pattern": pattern}
    return set_reservoir(network, ChangeSet(ps))

@router.post("/setreservoirx/", response_model=None)
async def fastapi_set_reservoir_x(network: str, reservoir: str, x: float) -> ChangeSet:
    ps = {"id": reservoir, "x": x}
    return set_reservoir(network, ChangeSet(ps))

@router.post("/setreservoiry/", response_model=None)
async def fastapi_set_reservoir_y(network: str, reservoir: str, y: float) -> ChangeSet:
    ps = {"id": reservoir, "y": y}
    return set_reservoir(network, ChangeSet(ps))

@router.post("/setreservoircoord/", response_model=None)
async def fastapi_set_reservoir_coord(
    network: str, reservoir: str, x: float, y: float
) -> ChangeSet:
    ps = {"id": reservoir, "x": x, "y": y}
    return set_reservoir(network, ChangeSet(ps))

@router.get("/getreservoirproperties/")
async def fastapi_get_reservoir_properties(
    network: str, reservoir: str
) -> dict[str, Any]:
    return get_reservoir(network, reservoir)

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

@router.post("/setreservoirproperties/", response_model=None)
async def fastapi_set_reservoir_properties(
    network: str, reservoir: str, req: Request
) -> ChangeSet:
    props = await req.json()
    ps = {"id": reservoir} | props
    return set_reservoir(network, ChangeSet(ps))
