from fastapi import APIRouter, Request
from typing import Any, List, Dict, Union
from app.services.tjnetwork import (
    Any,
    ChangeSet,
    add_pattern,
    delete_pattern,
    get_pattern,
    get_pattern_schema,
    get_patterns,
    is_pattern,
    set_pattern,
)

router = APIRouter()

@router.get("/getpatternschema")
async def fastapi_get_pattern_schema(network: str) -> dict[str, dict[str, Any]]:
    return get_pattern_schema(network)

@router.post("/addpattern/", response_model=None)
async def fastapi_add_pattern(network: str, pattern: str, req: Request) -> ChangeSet:
    props = await req.json()
    ps = {
        "id": pattern,
    } | props
    return add_pattern(network, ChangeSet(ps))

@router.post("/deletepattern/", response_model=None)
async def fastapi_delete_pattern(network: str, pattern: str) -> ChangeSet:
    ps = {"id": pattern}
    return delete_pattern(network, ChangeSet(ps))

@router.get("/getpatternproperties/")
async def fastapi_get_pattern_properties(network: str, pattern: str) -> dict[str, Any]:
    return get_pattern(network, pattern)

@router.post("/setpatternproperties/", response_model=None)
async def fastapi_set_pattern_properties(
    network: str, pattern: str, req: Request
) -> ChangeSet:
    props = await req.json()
    ps = {"id": pattern} | props
    return set_pattern(network, ChangeSet(ps))

@router.get("/ispattern/")
async def fastapi_is_pattern(network: str, pattern: str) -> bool:
    return is_pattern(network, pattern)

@router.get("/getpatterns/")
async def fastapi_get_patterns(network: str) -> list[str]:
    return get_patterns(network)
