from pathlib import Path
import pytest as pt

from src.main.python.ddadevops.release_mixin.infrastructure_api import GitApi, EnvironmentApi, JsonFileHandler
from src.main.python.ddadevops.release_mixin.infrastructure import VersionRepository
from src.main.python.ddadevops.release_mixin.domain import ReleaseType

from .helper import Helper

def change_test_dir( tmp_path: Path, monkeypatch: pt.MonkeyPatch):
    monkeypatch.chdir(tmp_path)

def test_environment_api():
    # init 
    env_api = EnvironmentApi()
    key = "TEST_ENV_KEY"
    value = "data"
    env_api.set(key, value)

    #test 
    assert env_api.get(key) == value

def test_git_api(tmp_path: Path, monkeypatch: pt.MonkeyPatch):
    # init
    th = Helper()
    th.copy_files(th.TEST_FILE_PATH, tmp_path)

    # change the context of the script execution to tmp_path
    change_test_dir(tmp_path, monkeypatch) 

    git_api = GitApi()
    git_api.init()
    git_api.set_user_config("ex.ample@mail.com", "Ex Ample")
    git_api.add_file(th.TEST_FILE_NAME)
    git_api.commit("MINOR release")

    # test
    latest_commit = git_api.get_latest_commit()
    assert "MINOR release" in latest_commit

# file handler tests
def test_gradle(tmp_path):
    # init
    th = Helper('config.gradle')
    th.copy_files(th.TEST_FILE_PATH, tmp_path)
    th.TEST_FILE_PATH = tmp_path / th.TEST_FILE_NAME

    # test
    repo = VersionRepository(th.TEST_FILE_PATH)
    version = repo.get_version()
    version = version.create_release_version(ReleaseType.SNAPSHOT)
    repo.write_file(version.get_version_string())

    # check
    assert 'version = "12.4.678-SNAPSHOT"' in th.TEST_FILE_PATH.read_text()


def test_json(tmp_path):
    # init
    th = Helper('config.json')
    th.copy_files(th.TEST_FILE_PATH, tmp_path)
    th.TEST_FILE_PATH = tmp_path / th.TEST_FILE_NAME

    # test
    repo = VersionRepository(th.TEST_FILE_PATH)
    version = repo.get_version()
    version = version.create_release_version(ReleaseType.SNAPSHOT)
    repo.write_file(version.get_version_string())

    # check
    assert '"version": "123.123.456-SNAPSHOT"' in th.TEST_FILE_PATH.read_text()


def test_clojure(tmp_path):
    # init
    th = Helper('config.clj')
    th.copy_files(th.TEST_FILE_PATH, tmp_path)
    th.TEST_FILE_PATH = tmp_path / th.TEST_FILE_NAME

    # test
    repo = VersionRepository(th.TEST_FILE_PATH)
    version = repo.get_version()
    version = version.create_release_version(ReleaseType.SNAPSHOT)
    repo.write_file(version.get_version_string())

    # check
    assert '1.1.3-SNAPSHOT' in th.TEST_FILE_PATH.read_text()


def test_python(tmp_path):
    # init
    th = Helper('config.py')
    th.copy_files(th.TEST_FILE_PATH, tmp_path)
    th.TEST_FILE_PATH = tmp_path / th.TEST_FILE_NAME

    # test
    repo = VersionRepository(th.TEST_FILE_PATH)
    version = repo.get_version()
    version = version.create_release_version(ReleaseType.SNAPSHOT)
    repo.write_file(version.get_version_string())

    # check
    assert '3.1.3-SNAPSHOT' in th.TEST_FILE_PATH.read_text()