#!/usr/bin/env python3
import os
import pathlib
import re
import sys
import time

import packaging.version
import requests

HEADERS = {"Authorization": f"token {os.environ.get('READTHEDOCS_TOKEN')}"}
PROJECT = "praw"


def fetch_versions():
    response = requests.get(
        f"https://readthedocs.org/api/v3/projects/{PROJECT}/versions?active=true",
        headers=HEADERS,
    )
    versions = None
    if response.status_code == 200:
        active_versions = response.json()
        versions = [
            packaging.version.parse(slug["slug"].strip("v"))
            for slug in active_versions["results"]
            if not slug["hidden"] and slug["slug"] not in ["stable", "latest"]
        ]
    if versions is None:
        sys.stderr.write("Failed to get current active versions\n")
    return versions


def main():
    content = pathlib.Path(f"{PROJECT}/const.py").read_text()
    current_version = packaging.version.parse(re.search(r'__version__ = "([^"]+)"', content).group(1))
    if current_version.is_devrelease:
        current_version = packaging.version.parse(
            f"{current_version.major}.{current_version.minor}.{current_version.micro - 1}"
        )
    max_retry_count = 5
    retry_count = 0
    while True:
        # Versions are not active by default; activate the version once Read
        # the Docs has synced the tag.
        response = requests.patch(
            f"https://readthedocs.org/api/v3/projects/{PROJECT}/versions/v{current_version}/",
            headers=HEADERS,
            json={"active": True, "hidden": False},
        )
        if response.status_code == 204:
            sys.stdout.write(f"Version {current_version!s} is active\n")
            versions = fetch_versions()
            if versions is None:
                return 1
            if current_version in versions:
                break
        if retry_count >= max_retry_count:
            sys.stderr.write(f"Current version {current_version!s} failed to activate\n")
            return 1
        sys.stdout.write("Waiting 30 seconds for version to activate\n")
        retry_count += 1
        time.sleep(30)
    aggregated_versions = {}
    for version in versions:
        aggregated_versions.setdefault(version.major, [])
        aggregated_versions[version.major].append(version)

    latest_major_versions = [max(aggregated_versions[major]) for major in aggregated_versions]
    major_versions = [version.major for version in versions]
    is_new_major = major_versions.count(current_version.major) == 1

    for version in versions:
        if (is_new_major and version not in latest_major_versions) or (
            (version.major, version.minor)
            == (
                current_version.major,
                current_version.minor,
            )
            and version.micro != current_version.micro
        ):
            response = requests.patch(
                f"https://readthedocs.org/api/v3/projects/{PROJECT}/versions/v{version}/",
                headers=HEADERS,
                json={"active": True, "hidden": True},
            )
            if response.status_code == 204:
                sys.stderr.write(f"Version {version!s} was hidden successfully\n")
            else:
                sys.stderr.write(f"Failed to hide version {version!s}\n")
                return 1
    return 0


if __name__ == "__main__":
    sys.exit(main())
