from __future__ import annotations

from dataclasses import dataclass
from pathlib import Path
from typing import Optional

from .settings import GatewaySettings, get_gateway_settings


@dataclass
class GatewayConfig:
    postgres_dsn: str | None = None
    redis_url: str | None = None
    signing_secret: str | None = None
    region: str | None = None
    auto_init: bool = False
    settings: GatewaySettings | None = None

    @classmethod
    def from_settings(cls, settings: GatewaySettings) -> "GatewayConfig":
        return cls(
            postgres_dsn=settings.stores.postgres_dsn,
            redis_url=settings.stores.redis_url,
            signing_secret=settings.secrets.signing_secret,
            region=settings.runtime.region,
            auto_init=settings.stores.auto_init,
            settings=settings,
        )

    @classmethod
    def load(cls, path: Optional[Path] = None) -> "GatewayConfig":
        settings = get_gateway_settings(path)
        return cls.from_settings(settings)
