plugin_package_json=plugin.json
json_plugin_id := $(shell cat $(plugin_package_json) | python3 -c "import sys, json; print(str(json.load(sys.stdin)['id']).replace('/',''))")
json_plugin_version := $(shell cat $(plugin_package_json) | python3 -c "import sys, json; print(str(json.load(sys.stdin)['version']).replace('/',''))")

plugin_id ?= $(json_plugin_id)
plugin_version ?= $(json_plugin_version)

$(info Using plugin version: $(plugin_version))
$(info Using plugin id: $(plugin_id))
archive_file_name="dss-plugin-${plugin_id}-${plugin_version}.zip"

.DEFAULT_GOAL := plugin

plugin: dist-clean
	@echo "[START] Archiving plugin to dist/ folder..."
	@cat plugin.json | json_pp > /dev/null
	@mkdir dist
	@echo "{\"remote_url\":\"${remote_url}\",\"last_commit_id\":\"${last_commit_id}\"}" > release_info.json
	@git archive -v -9 --format zip -o dist/${archive_file_name} HEAD
	@if [[ -d tests ]]; then \
		zip --delete dist/${archive_file_name} "tests/*"; \
	fi
	@zip -u dist/${archive_file_name} release_info.json
	@rm release_info.json
	@if [ "$(add_tag)" = "true" ]; then \
        $(MAKE) add-git-tag; \
		$(MAKE) tagged-release; \
	fi
	@echo "[SUCCESS] Archiving plugin to dist/${archive_file_name} folder: Done!🌟"

.PHONY: add-git-tag
add-git-tag:
	@echo "Adding tag v${plugin_version} to main repository"
	@git tag -a "v${plugin_version}" -m "Tagging version ${plugin_version}"
	@git push origin "v${plugin_version}"

.PHONY: tagged-release
tagged-release:
	@echo "Creating GitHub release and attaching archive..."
	@gh release create "v${plugin_version}" dist/${archive_file_name} \
	    --title "Version ${plugin_version}" \
		--generate-notes


dev: dist-clean
	@echo "[START] Archiving plugin to dist/ folder... (dev mode)"
	@cat plugin.json | json_pp > /dev/null
	@mkdir dist
	@zip -v -9 dist/${archive_file_name} -r . --exclude "tests/*" "env/*" ".git/*" ".pytest_cache/*" ".idea/*" "dist/*"
	@echo "[SUCCESS] Archiving plugin to dist/ folder: Done!"

# unit-tests:
# 	@echo "Running unit tests..."
# 	@( \
# 		PYTHON_VERSION=`python3 -c "import sys; print('PYTHON{}{}'.format(sys.version_info.major, sys.version_info.minor))"`; \
# 		PYTHON_VERSION_IS_CORRECT=`cat code-env/python/desc.json | python3 -c "import sys, json; print('$$PYTHON_VERSION' in json.load(sys.stdin)['acceptedPythonInterpreters']);"`; \
# 		if [ $$PYTHON_VERSION_IS_CORRECT == "False" ]; then echo "Python version $$PYTHON_VERSION is not in acceptedPythonInterpreters"; exit 1; else echo "Python version $$PYTHON_VERSION is in acceptedPythonInterpreters"; fi; \
# 	)
# 	@( \
# 		rm -rf ./env/; \
# 		python3 -m venv env/; \
# 		source env/bin/activate; \
# 		pip install --upgrade pip;\
# 		pip install --no-cache-dir -r tests/python/unit/requirements.txt; \
# 		pip install --no-cache-dir -r code-env/python/spec/requirements.txt; \
# 		export PYTHONPATH="$(PYTHONPATH):$(PWD)/python-lib"; \
# 		python3 -m pytest tests/python/unit --alluredir=tests/allure_report || ret=$$?; exit $$ret \
# 	)

# integration-tests:
# 	@echo "Running integration tests..."
# 	@( \
# 		rm -rf ./env/; \
# 		python3 -m venv env/; \
# 		source env/bin/activate; \
# 		pip3 install --upgrade pip;\
# 		pip install --no-cache-dir -r tests/python/integration/requirements.txt; \
# 		python3 -m pytest tests/python/integration --alluredir=tests/allure_report || ret=$$?; exit $$ret \
# 	)

# tests: unit-tests integration-tests

dist-clean:
	rm -rf dist

mypy:
	@echo "Running mypy..."
	@mypy -p python-lib -p python-agent-tools --config-file=pyproject.toml

lint:
	@echo "Running ruff ..."
	@ruff check python-lib python-agent-tools eval-tool

unit:
	@echo "Running unit tests..."
	@PYTHONPATH="$(PYTHONPATH):$(PWD)/python-lib" pytest --cov=python-lib tests/python/unit

tests: mypy lint unit


.PHONY: update-plugin-json-version
update-plugin-json-version:
	@current_plugin_version=`cat ${plugin_package_json} | python3 -c "import sys, json; print(str(json.load(sys.stdin)['version']))"`
	@if [ "${plugin_version}" != "$$current_plugin_version" ]; then \
		cp ${plugin_package_json} ${plugin_package_json}.bak; \
		echo "Updating ${plugin_package_json} with new version ${plugin_version}"; \
		sed "s/\"version\": \".*\"/\"version\": \"${plugin_version}\"/" ${plugin_package_json}.bak > ${plugin_package_json}; \
		rm ${plugin_package_json}.bak; \
	else \
		echo "${plugin_package_json} is already at version ${plugin_version}"; \
		exit 1; \
	fi


.PHONY: update-changelog
update-changelog:
	@echo "Updating changelog with version $(plugin_version)"
	@DATE=$$(date +%Y-%m-%d); \
	awk -v ver="## [Version $(plugin_version)] - Fix/Feature release - $$DATE" \
		'NR==1 { print $$0; print ""; print ver; print "- PLEASE INSERT FEATURE AND FIXES"; next } \
		 { print }' CHANGELOG.md > CHANGELOG.tmp && mv CHANGELOG.tmp CHANGELOG.md
	@echo "Changelog updated. Please edit CHANGELOG.md to add features and fixes for version $(plugin_version). Remember to use Fix or Feature as appropriate"
bump:
	@echo "Bumping plugin version to $(plugin_version)"
	@$(MAKE) update-plugin-json-version
	@$(MAKE) update-changelog
