"""Delta Lake settings."""

from __future__ import annotations

from functools import lru_cache
from typing import Any

from pydantic import AliasChoices, Field

from phlo.config.base import BaseConfig
from phlo.config.network import resolve_url


class DeltaSettings(BaseConfig):
    """Delta Lake storage configuration."""

    delta_warehouse_path: str = Field(
        default="s3://lake/warehouse/delta", description="S3 path for Delta tables"
    )
    delta_staging_path: str = Field(
        default="s3://lake/stage", description="S3 path for staging parquet files"
    )
    delta_default_namespace: str = Field(
        default="raw", description="Default namespace/schema for Delta tables"
    )
    delta_s3_endpoint: str | None = Field(
        default="http://localhost:9000",
        validation_alias=AliasChoices("DELTA_S3_ENDPOINT", "AWS_S3_ENDPOINT"),
        description="S3 endpoint URL for Delta I/O",
    )
    delta_s3_access_key: str = Field(
        default="minio",
        validation_alias=AliasChoices("DELTA_S3_ACCESS_KEY", "AWS_ACCESS_KEY_ID"),
        description="S3 access key for Delta I/O",
    )
    delta_s3_secret_key: str = Field(
        default="minio123",
        validation_alias=AliasChoices("DELTA_S3_SECRET_KEY", "AWS_SECRET_ACCESS_KEY"),
        description="S3 secret key for Delta I/O",
    )
    delta_s3_region: str = Field(
        default="us-east-1",
        validation_alias=AliasChoices("DELTA_S3_REGION", "AWS_REGION"),
        description="S3 region for Delta I/O",
    )
    delta_s3_allow_unsafe_rename: bool = Field(
        default=True,
        description="Allow unsafe rename for S3 (non-HDFS) backends",
    )

    def model_post_init(self, __context: Any) -> None:
        if self.delta_s3_endpoint:
            resolved = resolve_url(self.delta_s3_endpoint, port_env_var="MINIO_API_PORT")
            object.__setattr__(self, "delta_s3_endpoint", resolved)

    def get_storage_options(self) -> dict[str, str]:
        """Build deltalake storage options dict for S3 access.

        Returns:
            dict[str, str]: Storage options compatible with deltalake I/O.
        """
        opts: dict[str, str] = {
            "AWS_ACCESS_KEY_ID": self.delta_s3_access_key,
            "AWS_SECRET_ACCESS_KEY": self.delta_s3_secret_key,
            "AWS_REGION": self.delta_s3_region,
            "AWS_ALLOW_HTTP": "true",
        }
        if self.delta_s3_endpoint:
            opts["AWS_ENDPOINT_URL"] = self.delta_s3_endpoint
        if self.delta_s3_allow_unsafe_rename:
            opts["AWS_S3_ALLOW_UNSAFE_RENAME"] = "true"
        return opts


@lru_cache(maxsize=1)
def get_settings() -> DeltaSettings:
    """Get cached Delta Lake settings.

    Returns:
        DeltaSettings: Cached Delta Lake settings instance.
    """
    return DeltaSettings()
