# generated by fastapi-codegen:
#   filename:  server.yaml
#   timestamp: 2024-01-05T05:18:11+00:00

from __future__ import annotations

from fastapi import APIRouter, HTTPException

from ..dependencies import *
from frequency.adapter import Adapter
from frequency.model import Model

router = APIRouter(tags=["Adapter"])


@router.post("/v1/adapters", response_model=V1Adapter, tags=["Adapter"])
def load_adapter(body: V1Adapter = None) -> V1Adapter:
    """
    Load an adapter
    """
    adapter = Adapter.from_v1_schema(body)
    print("finding model: ", body.__dict__)
    model = Model.find(body.model)
    print("found model: ", model)
    if not model:
        return HTTPException(status_code=404, detail="model not found")
    model.add_adapter(adapter)
    return adapter.to_v1_schema()


@router.get("/v1/adapters", response_model=V1Adapters, tags=["Adapter"])
def get_adapters() -> V1Adapters:
    """
    A list of adapters
    """
    return Adapter.list_v1()


@router.get("/v1/adapters/{name}", response_model=V1Adapter, tags=["Adapter"])
def get_adapter(name: str) -> V1Adapter:
    """
    Get an adapter
    """
    adapter = Adapter.find(name)
    if not adapter:
        return HTTPException(status_code=404, detail="adapter not found")

    return adapter.to_v1_schema()


@router.delete("/v1/adapters/{name}", response_model=None, tags=["Adapter"])
def delete_adapter(name: str) -> None:
    """
    Delete an adapter
    """
    Adapter.delete(name)
