# global
import os
from hypothesis import settings, HealthCheck, Phase
from hypothesis.database import (
    MultiplexedDatabase,
    ReadOnlyDatabase,
    DirectoryBasedExampleDatabase,
)

hypothesis_cache = f"{os.getcwd()}/.hypothesis/examples/"
try:
    os.makedirs(hypothesis_cache)
except FileExistsError:
    pass


def pytest_terminal_summary(terminalreporter):
    from .test_ivy.conftest import mod_backend

    session = terminalreporter._session

    if session.testscollected == 0:
        return

    passed_ratio = 1 - (session.testsfailed / session.testscollected)
    text = f" {passed_ratio:.1%} of {session.testscollected} passed "
    text = text.center(terminalreporter._screen_width, "=")
    terminalreporter.write(content=text)
    for key in mod_backend:
        if mod_backend[key]:
            mod_backend[key][0].terminate()


def pytest_addoption(parser):
    parser.addoption(
        "-N",
        "--num-examples",
        action="store",
        default=15,
        type=int,
        help="set max examples generated by Hypothesis",
    )
    parser.addoption(
        "--deadline",
        action="store",
        default=100_000,
        type=int,
        help="set deadline for testing one example",
    )
    parser.addoption(
        "--ivy-tb",
        action="store",
        default="full",
        type=str,
        help="ivy traceback",
    )
    parser.addoption(
        "--reuse-only",
        default=False,
        action="store_true",
        help="Only reuse stored examples from database",
    )
    parser.addoption(
        "-R",
        "--robust",
        action="store_true",
        default=False,
        help=(
            "Disable Hypothesis Shrinking. Allow all Hypothesis HealthChecks."
            "Disabling the HealthChecks will most likely introduce new failures, "
            "this mode should be only used during development on the testing pipeline."
        ),
    )


def pytest_configure(config):
    profile_settings = {}
    getopt = config.getoption
    max_examples = getopt("--num-examples")
    deadline = getopt("--deadline")

    profile_settings["database"] = DirectoryBasedExampleDatabase(
        path=hypothesis_cache
    )

    if max_examples:
        profile_settings["max_examples"] = max_examples
    else:
        profile_settings["max_examples"] = 10
    if deadline:
        profile_settings["deadline"] = deadline

    if getopt("--reuse-only"):
        profile_settings["phases"] = [Phase.explicit, Phase.reuse]

    settings.register_profile(
        "ivy_profile",
        **profile_settings,
        suppress_health_check=(HealthCheck(3), HealthCheck(2), HealthCheck(1)),
        phases=[Phase.explicit, Phase.reuse, Phase.generate],
        print_blob=True,
    )

    settings.register_profile(
        "robust",
        phases=[Phase.explicit, Phase.reuse, Phase.generate, Phase.target],
    )

    settings.register_profile(
        "diff",
        database=None,
        derandomize=True,
        max_examples=100,
        deadline=5000,
        phases=[Phase.generate],
        suppress_health_check=(HealthCheck(3), HealthCheck(2), HealthCheck(1)),
    )

    if getopt("robust"):
        settings.load_profile("robust")
    else:
        settings.load_profile("ivy_profile")
