# Project Variables
ENV_FILE=.env
ENV_EXAMPLE=.env.example
DOCKER_COMPOSE=docker compose
PROJECT_NAME=plant_price_tracker

# Commands
.PHONY: help up down rebuild logs status seed shell dbshell reset test

help:
	@echo "Available targets:"
	@echo "  up         - Build and start the app (bootstraps .env if needed)"
	@echo "  down       - Stop and remove containers"
	@echo "  rebuild    - Rebuild containers from scratch"
	@echo "  logs       - Show logs for all services"
	@echo "  status     - Show container health status"
	@echo "  seed       - Manually seed the database"
	@echo "  shell      - Open a bash shell in the web container"
	@echo "  dbshell    - Open a MySQL shell"
	@echo "  reset      - Nuke everything and restart clean"
	@echo "  test       - Run test suite (TBD)"

up:
	@if [ ! -f $(ENV_FILE) ]; then \
		echo "[⚙] Generating .env from example..."; \
		cp $(ENV_EXAMPLE) $(ENV_FILE); \
	fi
	$(DOCKER_COMPOSE) up --build -d
	@$(MAKE) wait

up-nobuild:
	@if [ ! -f $(ENV_FILE) ]; then \
		echo "[⚙] Generating .env from example..."; \
		cp $(ENV_EXAMPLE) $(ENV_FILE); \
	fi
	$(DOCKER_COMPOSE) up -d
	@$(MAKE) wait

down:
	$(DOCKER_COMPOSE) down

rebuild:
	$(DOCKER_COMPOSE) down -v --remove-orphans
	$(DOCKER_COMPOSE) build
	$(DOCKER_COMPOSE) up -d db
	sleep 5  # give MySQL a moment to initialize
	$(DOCKER_COMPOSE) exec -T db mysql -uroot -p$$(grep MYSQL_ROOT_PASSWORD .env | cut -d '=' -f2) -e "DROP DATABASE IF EXISTS natureinpots; CREATE DATABASE natureinpots CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
	$(DOCKER_COMPOSE) up --build -d
	@$(MAKE) wait

preload:
	@docker exec -it $$(docker ps -qf "name=$(PROJECT_NAME)-web") flask preload-data

logs:
	$(DOCKER_COMPOSE) logs -f

status:
	@echo "[📊] Health status of containers:"
	@docker ps --filter "name=$(PROJECT_NAME)-" --format "table {{.Names}}\t{{.Status}}"

seed:
	@docker exec -it $$(docker ps -qf "name=$(PROJECT_NAME)-web") flask preload-data

shell:
	@docker exec -it $$(docker ps -qf "name=$(PROJECT_NAME)-web") bash

dbshell:
	@docker exec -it $$(docker ps -qf "name=$(PROJECT_NAME)-db") \
		mysql -u$$(grep MYSQL_USER $(ENV_FILE) | cut -d '=' -f2) \
		-p$$(grep MYSQL_PASSWORD $(ENV_FILE) | cut -d '=' -f2) \
		$$(grep MYSQL_DATABASE $(ENV_FILE) | cut -d '=' -f2)

reset:
	@echo "[💣] Nuking containers and volumes..."
	$(DOCKER_COMPOSE) down -v --remove-orphans
	@echo "[🧼] Cleaning caches and migrations..."
	sudo rm -rf __pycache__ */__pycache__ */*/__pycache__ *.pyc *.pyo *.db migrations
	@echo "[🚀] Rebuilding project fresh..."
	@$(MAKE) up

test:
	@echo "[🚧] Test suite placeholder"
	# insert pytest or unittest commands here

wait:
	@echo "[⏳] Waiting for web container to be healthy..."
	@timeout 30 bash -c '\
		WEB_CONTAINER=$$($(DOCKER_COMPOSE) ps -q web); \
		if [ -z "$$WEB_CONTAINER" ]; then \
			echo "[❌] Could not detect web container!"; \
			exit 1; \
		fi; \
		echo "[ℹ] Detected container: $$WEB_CONTAINER"; \
		while [ "$$(docker inspect -f "{{.State.Health.Status}}" $$WEB_CONTAINER 2>/dev/null)" != "healthy" ]; do \
			sleep 2; echo -n "."; \
		done; echo "\n[✅] $$WEB_CONTAINER is healthy!"'

migrate:
	flask db migrate -m "auto"

upgrade:
	flask db upgrade

