from fastapi import APIRouter, Request
from typing import Any, List, Dict, Union
from app.services.tjnetwork import (
    Any,
    ChangeSet,
    add_mixing,
    add_source,
    api,
    delete_mixing,
    delete_source,
    get_emitter,
    get_emitter_schema,
    get_mixing,
    get_mixing_schema,
    get_pipe_reaction,
    get_pipe_reaction_schema,
    get_quality,
    get_quality_schema,
    get_reaction,
    get_reaction_schema,
    get_source,
    get_source_schema,
    get_tank_reaction,
    get_tank_reaction_schema,
    set_emitter,
    set_pipe_reaction,
    set_quality,
    set_reaction,
    set_source,
    set_tank_reaction,
)

router = APIRouter()

@router.get("/getqualityschema/")
async def fastapi_get_quality_schema(network: str) -> dict[str, dict[str, Any]]:
    return get_quality_schema(network)

@router.get("/getqualityproperties/")
async def fastapi_get_quality_properties(network: str, node: str) -> dict[str, Any]:
    return get_quality(network, node)

@router.post("/setqualityproperties/", response_model=None)
async def fastapi_set_quality_properties(network: str, req: Request) -> ChangeSet:
    props = await req.json()
    return set_quality(network, ChangeSet(props))

@router.get("/getemitterschema")
async def fastapi_get_emitter_schema(network: str) -> dict[str, dict[str, Any]]:
    return get_emitter_schema(network)

@router.get("/getemitterproperties/")
async def fastapi_get_emitter_properties(network: str, junction: str) -> dict[str, Any]:
    return get_emitter(network, junction)

@router.post("/setemitterproperties/", response_model=None)
async def fastapi_set_emitter_properties(
    network: str, junction: str, req: Request
) -> ChangeSet:
    props = await req.json()
    ps = {"junction": junction} | props
    return set_emitter(network, ChangeSet(ps))

@router.get("/getsourcechema/")
async def fastapi_get_source_schema(network: str) -> dict[str, dict[str, Any]]:
    return get_source_schema(network)

@router.get("/getsource/")
async def fastapi_get_source(network: str, node: str) -> dict[str, Any]:
    return get_source(network, node)

@router.post("/setsource/", response_model=None)
async def fastapi_set_source(network: str, req: Request) -> ChangeSet:
    props = await req.json()
    return set_source(network, ChangeSet(props))

@router.post("/addsource/", response_model=None)
async def fastapi_add_source(network: str, req: Request) -> ChangeSet:
    props = await req.json()
    return add_source(network, ChangeSet(props))

@router.post("/deletesource/", response_model=None)
async def fastapi_delete_source(network: str, node: str) -> ChangeSet:
    props = {"node": node}
    return delete_source(network, ChangeSet(props))

@router.get("/getreactionschema/")
async def fastapi_get_reaction_schema(network: str) -> dict[str, dict[str, Any]]:
    return get_reaction_schema(network)

@router.get("/getreaction/")
async def fastapi_get_reaction(network: str) -> dict[str, Any]:
    return get_reaction(network)

@router.post("/setreaction/", response_model=None)
async def fastapi_set_reaction(network: str, req: Request) -> ChangeSet:
    props = await req.json()
    return set_reaction(network, ChangeSet(props))

@router.get("/getpipereactionschema/")
async def fastapi_get_pipe_reaction_schema(network: str) -> dict[str, dict[str, Any]]:
    return get_pipe_reaction_schema(network)

@router.get("/getpipereaction/")
async def fastapi_get_pipe_reaction(network: str, pipe: str) -> dict[str, Any]:
    return get_pipe_reaction(network, pipe)

@router.post("/setpipereaction/", response_model=None)
async def fastapi_set_pipe_reaction(network: str, req: Request) -> ChangeSet:
    props = await req.json()
    return set_pipe_reaction(network, ChangeSet(props))

@router.get("/gettankreactionschema/")
async def fastapi_get_tank_reaction_schema(network: str) -> dict[str, dict[str, Any]]:
    return get_tank_reaction_schema(network)

@router.get("/gettankreaction/")
async def fastapi_get_tank_reaction(network: str, tank: str) -> dict[str, Any]:
    return get_tank_reaction(network, tank)

@router.post("/settankreaction/", response_model=None)
async def fastapi_set_tank_reaction(network: str, req: Request) -> ChangeSet:
    props = await req.json()
    return set_tank_reaction(network, ChangeSet(props))

@router.get("/getmixingschema/")
async def fastapi_get_mixing_schema(network: str) -> dict[str, dict[str, Any]]:
    return get_mixing_schema(network)

@router.get("/getmixing/")
async def fastapi_get_mixing(network: str, tank: str) -> dict[str, Any]:
    return get_mixing(network, tank)

@router.post("/setmixing/", response_model=None)
async def fastapi_set_mixing(network: str, req: Request) -> ChangeSet:
    props = await req.json()
    return api.set_mixing(network, ChangeSet(props))

@router.post("/addmixing/", response_model=None)
async def fastapi_add_mixing(network: str, req: Request) -> ChangeSet:
    props = await req.json()
    return add_mixing(network, ChangeSet(props))

@router.post("/deletemixing/", response_model=None)
async def fastapi_delete_mixing(network: str, req: Request) -> ChangeSet:
    props = await req.json()
    return delete_mixing(network, ChangeSet(props))
