# Copyright (c) 2020 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
#

import platform
import subprocess


def get_os_info():
    """
    Returns operating system name and version information as a string.
    Works across Windows, macOS, and Linux platforms.

    Returns:
        str: A string with format "os_name/os_version"
    """
    os_name = platform.system().lower()
    os_version = "unknown"
    if os_name == "darwin":
        # macOS - use platform.mac_ver() to get the version
        os_name = "macos"
        os_version = platform.mac_ver()[0]
    elif os_name == "windows":
        # Windows - use platform.win32_ver() to get the version
        os_version = platform.win32_ver()[0]
    elif os_name == "linux":
        # Linux - try several methods to get distribution and version
        # First try os-release file
        try:
            with open("/etc/os-release") as f:
                lines = f.readlines()
                for line in lines:
                    if line.startswith("ID="):
                        os_name = line.split("=")[1].strip().strip('"')
                    elif line.startswith("VERSION_ID="):
                        os_version = line.split("=")[1].strip().strip('"')
        except (FileNotFoundError, PermissionError):
            # Try using lsb_release command if available
            try:
                os_name = subprocess.check_output(["lsb_release", "-is"], text=True).strip().lower()
                os_version = subprocess.check_output(["lsb_release", "-rs"], text=True).strip()
            except (subprocess.SubprocessError, FileNotFoundError):
                # Fallback to platform.dist() for older Python versions
                if hasattr(platform, "dist"):
                    dist_info = platform.dist()
                    if dist_info[0]:
                        os_name = dist_info[0].lower()
                    if dist_info[1]:
                        os_version = dist_info[1]
    return f"{os_name}/{os_version}"
