from dataclasses import dataclass
from typing import Dict, List

from ._BaseModel import BaseModel
from ._ComponentsModel import ComponentsModel
from ._InfoModel import InfoModel
from ._PathModel import PathModel
from ._ServerModel import ServerModel
from ..utils import set_dict_path_value


@dataclass
class OpenapiModel(BaseModel):
    info: InfoModel
    paths: Dict[str, Dict[str, PathModel]] | None = None
    servers: List[ServerModel] | None = None
    components: ComponentsModel | None = None

    def add_path(self, url: str, method: str, path: PathModel) -> None:
        if self.paths is None:
            self.paths = {}
        set_dict_path_value(
            dict_value=self.paths,
            path=[url, method.lower()],
            value=path
        )

    def add_server(self, server: ServerModel) -> None:
        if self.servers is None:
            self.servers = []
        self.servers.append(server)

    def to_dict(self) -> dict:
        return {
            "openapi": "3.0.0",
            "info": self.info,
            "paths": self.paths,
            "servers": self.servers,
            "components": self.components,
        }
