# Makefile for Nextcloud Stack Deployment
# Provides convenient shortcuts for common Ansible operations

.PHONY: help setup ping check deploy deploy-stack deploy-dry update backup logs status restart rollback clean edit-vault rekey-vault

help:  ## Show this help message
	@echo "Nextcloud Stack Deployment - Make Commands"
	@echo ""
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'

setup:  ## Run interactive setup script
	@./setup.sh

ping:  ## Test connectivity to all servers
	@ansible all -m ping --ask-vault-pass

check:  ## Run preflight checks only
	@ansible-playbook playbooks/01-preflight-checks.yml --ask-vault-pass

deploy:  ## Full deployment (all playbooks)
	@ansible-playbook playbooks/site.yml --ask-vault-pass

deploy-stack:  ## Deploy stack only (skip system setup)
	@ansible-playbook playbooks/05-deploy-stack.yml --ask-vault-pass

deploy-dry:  ## Dry run (check mode)
	@ansible-playbook playbooks/site.yml --ask-vault-pass --check

update:  ## Update Docker images (safe services only)
	@ansible all -m shell -a "cd /opt/nextcloud-stack && docker compose pull && docker compose up -d" --ask-vault-pass

backup:  ## Run manual backup on all servers
	@ansible all -m shell -a "/opt/nextcloud-stack/backup.sh" --ask-vault-pass

logs:  ## Tail logs from all containers
	@ansible all -m shell -a "cd /opt/nextcloud-stack && docker compose logs -f --tail=100" --ask-vault-pass

status:  ## Show container status
	@ansible all -m shell -a "cd /opt/nextcloud-stack && docker compose ps" --ask-vault-pass

restart:  ## Restart all containers
	@ansible all -m shell -a "cd /opt/nextcloud-stack && docker compose restart" --ask-vault-pass

rollback:  ## Emergency rollback
	@ansible-playbook playbooks/99-rollback.yml --ask-vault-pass

clean:  ## Remove generated inventory (keeps vault)
	@rm -f inventory/hosts.yml inventory/group_vars/all/vars.yml
	@echo "Cleaned generated files (vault.yml preserved)"

edit-vault:  ## Edit encrypted vault
	@ansible-vault edit inventory/group_vars/all/vault.yml

rekey-vault:  ## Change vault password
	@ansible-vault rekey inventory/group_vars/all/vault.yml

inventory:  ## Show inventory configuration
	@ansible-inventory --list

graph:  ## Show inventory graph
	@ansible-inventory --graph
