from fastapi import APIRouter, Request, Response
from typing import Any, List, Dict, Union
from app.services.tjnetwork import (
    Any,
    ChangeSet,
    add_label,
    add_vertex,
    delete_label,
    delete_vertex,
    get_all_vertex_links,
    get_all_vertices,
    get_backdrop,
    get_backdrop_schema,
    get_label,
    get_label_schema,
    get_vertex,
    get_vertex_schema,
    set_backdrop,
    set_label,
    set_vertex,
)
from fastapi.responses import PlainTextResponse
import json

router = APIRouter()

@router.get("/getvertexschema/")
async def fastapi_get_vertex_schema(network: str) -> dict[str, dict[str, Any]]:
    return get_vertex_schema(network)

@router.get("/getvertexproperties/")
async def fastapi_get_vertex_properties(network: str, link: str) -> dict[str, Any]:
    return get_vertex(network, link)

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

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

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

@router.get("/getallvertexlinks/", response_class=PlainTextResponse)
async def fastapi_get_all_vertex_links(network: str) -> list[str]:
    return json.dumps(get_all_vertex_links(network))

@router.get("/getallvertices/", response_class=PlainTextResponse)
async def fastapi_get_all_vertices(network: str) -> list[dict[str, Any]]:
    return json.dumps(get_all_vertices(network))

@router.get("/getlabelschema/")
async def fastapi_get_label_schema(network: str) -> dict[str, dict[str, Any]]:
    return get_label_schema(network)

@router.get("/getlabelproperties/")
async def fastapi_get_label_properties(
    network: str, x: float, y: float
) -> dict[str, Any]:
    return get_label(network, x, y)

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

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

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

@router.get("/getbackdropschema/")
async def fastapi_get_backdrop_schema(network: str) -> dict[str, dict[str, Any]]:
    return get_backdrop_schema(network)

@router.get("/getbackdropproperties/")
async def fastapi_get_backdrop_properties(network: str) -> dict[str, Any]:
    return get_backdrop(network)

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