"""
Core environment management logic for dxlabs.
Handles the high-level business logic for pushing and pulling .env files.
"""

from ..config import Config
from ..providers.s3 import S3Provider


class EnvManager:
    """
    Manager for project environment (.env) files.
    Coordinates between the configuration and the storage provider.

    Attributes:
        config (Config): The project configuration.
        provider (S3Provider): The storage provider (currently S3).
    """

    def __init__(self, config: Config):
        """
        Initializes the EnvManager with a project configuration.

        Args:
            config (Config): The project settings (bucket, project ID, region, profile).
        """
        self.config = config
        self.provider = S3Provider(
            bucket=config.bucket, region=config.region, profile=config.profile
        )

    def get_remote_key(self, env_name: str) -> str:
        """
        Constructs the standard S3 key for an environment file.

        Format: {project_id}/{environment_name}/.env

        Args:
            env_name (str): The name of the environment (e.g., 'staging', 'dev').

        Returns:
            str: The fully qualified remote path/key.
        """
        return f"{self.config.project}/{env_name}/.env"

    def push(self, local_path: str, env_name: str) -> bool:
        """
        Pushes a local file to the remote storage for a given environment.

        Args:
            local_path (str): Path to the local file to upload.
            env_name (str): The environment name to target in the cloud.

        Returns:
            bool: True if the operation succeeded, False otherwise.
        """
        remote_key = self.get_remote_key(env_name)
        return self.provider.upload(local_path, remote_key)

    def pull(self, env_name: str, local_path: str) -> bool:
        """
        Pulls a file from remote storage to a local path for a given environment.

        Args:
            env_name (str): The environment name to pull from.
            local_path (str): The local destination where the file should be saved.

        Returns:
            bool: True if the operation succeeded, False otherwise.
        """
        remote_key = self.get_remote_key(env_name)
        return self.provider.download(remote_key, local_path)
