.PHONY: tf-init tf-plan tf-apply tf-destroy deploy setup-ansible

ANSIBLE_DIR = ansible
INVENTORY   = $(ANSIBLE_DIR)/inventory.ini

# ── Terraform ─────────────────────────────────────────────────────────────────

tf-init:
	terraform init

tf-plan:
	terraform plan

tf-apply:
	terraform apply
	@echo ""
	@echo "Server IP: $$(terraform output -raw server_ip)"
	@echo "Update $(INVENTORY) with this IP, then run: make deploy"

tf-destroy:
	@echo "WARNING: This will destroy the VM and all data on it."
	@read -p "Type 'yes' to confirm: " confirm && [ "$$confirm" = "yes" ] && terraform destroy

# ── Ansible ───────────────────────────────────────────────────────────────────

# First-time setup: waits for Docker, clones repo, starts stack.
setup:
	ansible-playbook -i $(INVENTORY) $(ANSIBLE_DIR)/site.yml --ask-vault-pass

# Update deploy: sync code + restart changed containers. Run this after every push.
deploy:
	ansible-playbook -i $(INVENTORY) $(ANSIBLE_DIR)/deploy.yml --ask-vault-pass

# ── Helpers ───────────────────────────────────────────────────────────────────

ip:
	@terraform output -raw server_ip

ssh:
	ssh drb@$$(terraform output -raw server_ip)
