import os

import pytest

from hawc.apps.animal.models import Endpoint
from hawc.apps.bmd.constants import SelectedModel
from hawc.apps.bmd.models import Session

SKIP_BMDS_TESTS = bool(os.environ.get("SKIP_BMDS_TESTS", "False") == "True")
IN_CI = os.environ.get("GITHUB_RUN_ID") is not None


@pytest.mark.django_db
@pytest.mark.skipif(SKIP_BMDS_TESTS or IN_CI, reason="BMDS execution environment unavailable")
class TestBmdSession:
    def test_dichotomous(self):
        # create new session w/ inputs
        session = Session.create_new(Endpoint.objects.get(id=8))
        assert session.version == "BMDS330"
        settings = session.get_settings()
        assert settings.version == 2
        assert settings.dtype == "D"

        # execute
        assert session.date_executed is None
        session.execute()
        assert session.date_executed is not None
        assert len(session.outputs["models"]) == 9

        # select model
        assert session.active is False
        selected = SelectedModel(model_index=1, notes="notes!")
        session.set_selected_model(selected)
        assert session.active is True
        assert session.selected["model_index"] == 1
        assert session.outputs["selected"] == {"model_index": 1, "notes": "notes!"}

    def test_continuous(self):
        # create new session w/ inputs
        session = Session.create_new(Endpoint.objects.get(id=3))
        assert session.version == "BMDS330"
        settings = session.get_settings()
        assert settings.version == 2
        assert settings.dtype == "C"

        # execute
        assert session.date_executed is None
        session.execute()
        assert session.date_executed is not None
        assert len(session.outputs["models"]) == 6

        # select model
        assert session.active is False
        selected = SelectedModel(model_index=2, notes="notes!")
        session.set_selected_model(selected)
        assert session.active is True
        assert session.selected["model_index"] == 2
        assert session.outputs["selected"] == {"model_index": 2, "notes": "notes!"}
