import requests
from dep_shield.models import Ecosystem

def get_latest_version(package_name: str, ecosystem: Ecosystem) -> str:
    """
    Fetches the latest version of a package from its respective registry.
    Default to "0.0.0" if not found (safely failing).
    """
    try:
        if ecosystem == Ecosystem.NPM:
            # NPM Registry API
            url = f"https://registry.npmjs.org/{package_name}/latest"
            resp = requests.get(url, timeout=3)
            if resp.status_code == 200:
                return resp.json().get("version", "0.0.0")
        
        elif ecosystem == Ecosystem.PYPI:
            # PyPI JSON API
            url = f"https://pypi.org/pypi/{package_name}/json"
            resp = requests.get(url, timeout=3)
            if resp.status_code == 200:
                return resp.json().get("info", {}).get("version", "0.0.0")

    except requests.RequestException:
        pass
    
    return "0.0.0"