import requests


def eza_get_latest_version():
    url = "https://api.github.com/repos/eza-community/eza/releases/latest"
    response = requests.get(url)
    if response.status_code == 200:
        latest_release = response.json()
        return latest_release['tag_name'].lstrip('v')
    return None


def eza_download_latest_version(version):
    download_url = f"https://github.com/eza-community/eza/releases/download/v{version}/eza_x86_64-unknown-linux-musl.zip"
    response = requests.get(download_url)
    if response.status_code == 200:
        with open(f"files/eza_x86_64-unknown-linux-musl.zip", "wb") as file:
            file.write(response.content)
        print(f"Downloaded eza version {version} (x86_64) successfully.")
    else:
        print(f"Failed to download eza version {version}.")
    download_url = f"https://github.com/eza-community/eza/releases/download/v{version}/eza_aarch64-unknown-linux-gnu.zip"
    response = requests.get(download_url)
    if response.status_code == 200:
        with open(f"files/eza_aarch64-unknown-linux-gnu.zip", "wb") as file:
            file.write(response.content)
        print(f"Downloaded eza version {version} (x86_64) successfully.")
    else:
        print(f"Failed to download eza version {version}.")


if __name__ == "__main__":
    latest_version = eza_get_latest_version()
    if (latest_version):
        eza_download_latest_version(latest_version)
    else:
        print("Failed to get the latest eza version.")
