.PHONY: help deploy_qa_streamlit drop_qa_streamlit_app run_streamlit_locally fmt lint run_local_etl create_snowflake_tasks vendor
.DEFAULT_GOAL := help

GET_SCHEMA = $(shell python3 -c "import json; print(json.load(open('app/configuration/connections.json'))['streamlit_backoffice_connection']['schema'])")

help:
	@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

lint: ## Run code linters
	uv run mypy app
	uv run ruff check
	uv run ruff format --check

fmt: ## Run code formatters
	uv run ruff check --fix
	uv run ruff format

test: ## Run unit tests
	uv run pytest tests/unit/ --snowflake-session=local

vendor: ## Re-download vendored wheels into app/vendor (run after upgrading vendored packages)
	pip download msoffcrypto-tool --no-deps -d /tmp/vendor_dl
	cd /tmp/vendor_dl && python3 -c "import zipfile, glob; [zipfile.ZipFile(w).extractall('$(CURDIR)/app/vendor') for w in glob.glob('*.whl')]"
	rm -rf app/vendor/*.dist-info app/vendor/NOTICE.txt

run_streamlit_locally: ## Run Streamlit app locally
	uv run streamlit run app/main.py

deploy_dev_streamlit_app: ## Deploy Streamlit app to DEV environment
	cd app && uv run snow streamlit deploy --replace --prune --connection=streamlit_backoffice_connection --schema=$(GET_SCHEMA) --env target=qa

drop_dev_streamlit_app: ## Drop Streamlit app and related stage in DEV environment
	uv run snow streamlit drop fansifter_backoffice_app --connection=streamlit_backoffice_connection --schema=$(GET_SCHEMA) && \
	uv run snow stage drop fansifter_backoffice_app_stage --connection=streamlit_backoffice_connection --schema=$(GET_SCHEMA)

qa_deploy_streamlit_app: ## Deploy Streamlit app to QA
	cd app && uv run snow streamlit deploy --replace --prune --connection=streamlit_backoffice_connection --schema=QA --env target=qa --role=FANSIFTER_ENGINEERING_PRIVACY

drop_qa_streamlit_app: ## Drop Streamlit app and related stage in QA environment
	uv run snow streamlit drop fansifter_backoffice_app --connection=streamlit_backoffice_connection --schema=QA --role=FANSIFTER_ENGINEERING_PRIVACY && \
	uv run snow stage drop fansifter_backoffice_app_stage --connection=streamlit_backoffice_connection --schema=QA --role=FANSIFTER_ENGINEERING_PRIVACY

# Deploy to Prod will be handled by Jenkins
