"""Environment variable configuration for the API plugin.

NOTE: Runs inside the Airflow api-server process. Do NOT import from src.*.
Mirrors settings from src/core/config.py via os.getenv().
"""

import os

UPLOAD_BUCKET_DEFAULT = "source-files"
MAX_UPLOAD_SIZE_MB_DEFAULT = 50
MAX_FILENAME_LENGTH_DEFAULT = 255
DOCSTORE_NAMESPACE_DEFAULT = "hierarchical"
PRESIGNED_URL_TTL_DEFAULT = 3600


def get_upload_bucket() -> str:
    return os.getenv("UPLOAD_BUCKET", UPLOAD_BUCKET_DEFAULT)


def get_max_upload_bytes() -> int:
    mb = int(os.getenv("MAX_UPLOAD_SIZE_MB", MAX_UPLOAD_SIZE_MB_DEFAULT))
    return mb * 1024 * 1024


def get_max_filename_length() -> int:
    return int(os.getenv("MAX_FILENAME_LENGTH", MAX_FILENAME_LENGTH_DEFAULT))


def get_presigned_url_ttl() -> int:
    return int(os.getenv("PRESIGNED_URL_TTL", PRESIGNED_URL_TTL_DEFAULT))


def get_presigned_s3_endpoint() -> str:
    """Public-facing S3 endpoint for presigned URLs.

    Must be the address the client (browser/APA) uses to reach SeaweedFS.
    In K8s this is the external Ingress/LB URL; in local dev it's localhost.
    """
    return os.getenv("PRESIGNED_S3_ENDPOINT", "http://localhost:8333")


def get_docstore_namespace() -> str:
    """Return the LlamaIndex docstore namespace as stored in the DB.

    LlamaIndex appends '/data' to the namespace passed to
    PostgresDocumentStore, so the value in the ``data_hierarchical_nodes``
    table is ``<namespace>/data``.
    """
    base = os.getenv("DOCSTORE_NAMESPACE", DOCSTORE_NAMESPACE_DEFAULT)
    return f"{base}/data"
