import os
import platform
from getpass import getuser

from packaging import version

from davidkhala.utils.syntax.path import join

APPDATA = {
    'Roaming': os.environ.get('APPDATA'),
    'Local': os.environ.get('LOCALAPPDATA')
}
USER = getuser()


def python_paths(major: str, minor: str) -> dict | None:
    if is_windows():
        home = join(APPDATA['Local'], 'Programs', 'Python', f"Python{major}{minor}")

        return {
            'home': home,
            'executable': join(home, 'python.exe'),
        }
    elif is_linux():
        home = '/usr/bin'
        return {
            'home': home,
            'executable': join(home, f"python{major}.{minor}"),
        }
    elif is_mac():
        home = f"/Library/Frameworks/Python.framework/Versions/{major}.{minor}/bin"
        return {
            "home": home,
            "executable": join(home, f"python{major}.{minor}"),
        }


class Version:
    """
    Python version
    """

    def __init__(self):
        self.major, self.minor, self.patch = platform.python_version_tuple()

    @staticmethod
    def sem_ver() -> str:
        return platform.python_version()

    @property
    def micro(self) -> str:
        return self.patch

    @staticmethod
    def is_older_than(target_version):
        """
        Check if the current Python version is older than the specified target version.

        :param target_version: A string representing the target version (e.g., "3.8.0").
        """
        current_version = Version.sem_ver()

        return version.parse(current_version) < version.parse(target_version)


def is_windows() -> bool:
    return os.name == 'nt' or platform.system() == 'Windows'


def is_linux() -> bool:
    return platform.system() == 'Linux'


def is_mac() -> bool:
    return platform.system() == 'Darwin'
