# Copyright (c) CloudZero - ALL RIGHTS RESERVED - PROPRIETARY AND CONFIDENTIAL
# Unauthorized copying of this file and/or project, via any medium is strictly prohibited.
# Direct all questions to legal@cloudzero.com
#

"""
Constants for the CZ-OPS CLI settings system.
"""

from src import __version__

CLI_VERSION = __version__

# File paths
_LEGACY_SETTINGS_FILENAME = "~/.cloudzero/ops_config"
_CZOPS_SETTINGS_FILENAME = "~/.cloudzero/czops.json"

# Version management constants
CURRENT_SETTINGS_VERSION = 30  # Current version for new settings files
MINIMUM_SUPPORTED_VERSION = 23  # Oldest version that can be auto-upgraded
LATEST_VERSION = CURRENT_SETTINGS_VERSION  # Alias for backward compatibility

# Version compatibility mapping: CLI version -> minimum required settings version
VERSION_COMPATIBILITY = {
    "0.37.0": 24,  # CLI 0.37.0 requires settings version 24+
    "0.38.0": 25,  # CLI 0.38.0 requires settings version 25+
    "0.39.0": 26,  # CLI 0.39.0 requires settings version 26+
    "0.40.0": 27,  # CLI 0.40.0 requires settings version 27+
    "1.0.0": 28,  # CLI 1.0.0 requires settings version 28+
}


def get_minimum_required_version(cli_version: str) -> int:
    """
    Get the minimum required settings version for a given CLI version.

    Args:
        cli_version: CLI version string (e.g., "0.37.12")

    Returns:
        Minimum required settings version
    """
    # Parse major.minor from version string
    parts = cli_version.split(".")
    if len(parts) >= 2:
        major_minor = f"{parts[0]}.{parts[1]}.0"
        return VERSION_COMPATIBILITY.get(major_minor, MINIMUM_SUPPORTED_VERSION)
    return MINIMUM_SUPPORTED_VERSION


def validate_settings_version(version: int, cli_version: str | None = None) -> bool:
    """
    Check if a settings version is compatible with the current CLI.

    Args:
        version: Settings file version
        cli_version: CLI version to check against (defaults to current)

    Returns:
        True if version is compatible, False otherwise
    """
    if cli_version is None:
        cli_version = CLI_VERSION

    min_required = get_minimum_required_version(cli_version)
    return min_required <= version <= CURRENT_SETTINGS_VERSION
