from dataclasses import dataclass
from pathlib import Path
from tomllib import loads
from typing import dataclass_transform

from adaptix import Retort


@dataclass_transform(frozen_default=True)
def to_configuration[ClsT](cls: type[ClsT]) -> type[ClsT]:
    return dataclass(frozen=True, slots=True, repr=False)(cls)


@to_configuration
class ServerConfiguration:
    host: str
    port: int
    access_log: bool

    @property
    def url(self) -> str:
        return f"http://{self.host}:{self.port}"


@to_configuration
class Configuration:
    server: ServerConfiguration


retort = Retort()


def load_configuration(path: Path) -> Configuration:
    with path.open("r", encoding="utf-8") as config:
        data = loads(config.read())

    return retort.load(data, Configuration)
