import os  # for testing purposes
from dataclasses import dataclass
from typing import Optional

import risc_lasagna
import risc_lasagna.layer as layer


@dataclass
class DatabaseConfig:
    host: str
    port: int
    user: Optional[str] = None
    password: Optional[str] = None


@dataclass
class ApplicationConfig:
    app_host: str
    app_port: int
    database_config: DatabaseConfig


def load_application_config() -> ApplicationConfig:
    os.environ["DATABASE_CONFIG_HOST"] = "db-host"
    os.environ["DATABASE_CONFIG_PORT"] = "9876"
    return risc_lasagna.build(
        ApplicationConfig,
        [
            layer.EnvironmentLayer(),
            layer.DataClassDefaultLayer(
                ApplicationConfig("localhost", 12345, DatabaseConfig("localhost", 5432))
            ),
        ],
    )


if __name__ == "__main__":
    config = load_application_config()
    print(config)
