from __future__ import annotations

from pydantic import BaseModel, ConfigDict, Field, RootModel
from typing_extensions import Annotated


class AddressLeaf(RootModel[str]):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    root: Annotated[str, Field(title="AddressLeaf")]
    """
    Target IP address
    """


class PortLeaf(RootModel[str]):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    root: Annotated[str, Field(title="PortLeaf")]
    """
    Target port number
    """


class DestinationContainer(BaseModel):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    address: Annotated[AddressLeaf, Field(None, alias="interfaces:address")]
    port: Annotated[PortLeaf, Field(None, alias="interfaces:port")]


class PeerContainer(BaseModel):
    model_config = ConfigDict(
        populate_by_name=True,
    )
    destination: Annotated[
        DestinationContainer, Field(None, alias="interfaces:destination")
    ]


class Model(BaseModel):
    """
    Initialize an instance of this class and serialize it to JSON; this results in a RESTCONF payload.

    ## Tips
    Initialization:
    - all values have to be set via keyword arguments
    - if a class contains only a `root` field, it can be initialized as follows:
        - `member=MyNode(root=<value>)`
        - `member=<value>`

    Serialziation:
    - `exclude_defaults=True` omits fields set to their default value (recommended)
    - `by_alias=True` ensures qualified names are used (necessary)
    """

    model_config = ConfigDict(
        populate_by_name=True,
    )
    peer: Annotated[PeerContainer, Field(None, alias="interfaces:peer")]


if __name__ == "__main__":
    model = Model(  # type: ignore[call-arg]
        # <Initialize model here>
    )

    restconf_payload = model.model_dump_json(
        exclude_defaults=True, by_alias=True, indent=2
    )

    print(f"Generated output: {restconf_payload}")

    # Send config to network device:
    # from pydantify.utility import restconf_patch_request
    # restconf_patch_request(url='...', user_pw_auth=('usr', 'pw'), data=restconf_payload)
