# Enterprise Infrastructure Simulator Makefile

.PHONY: help run demo up down patch destroy status logs clean test

# Default target
help: ## Show this help message
	@echo "Enterprise Infrastructure Simulator"
	@echo ""
	@echo "Available commands:"
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  %-15s %s\n", $$1, $$2}'

run: ## Run the default simulator workflow
	ansible-playbook -i inventory/hosts.ini playbooks/provision.yml

demo: ## Run a failure-and-patch demonstration
	./scripts/simulate_failure.sh service 30 web
	ansible-playbook -i inventory/hosts.ini playbooks/patch.yml

# Infrastructure management
up: ## Start the infrastructure simulation
	@echo "Starting enterprise infrastructure simulation..."
	docker-compose up -d
	@echo "Waiting for containers to be ready..."
	@sleep 30
	ansible-playbook -i inventory/hosts.ini playbooks/provision.yml
	@echo "Infrastructure simulation started successfully"

down: ## Stop the infrastructure simulation
	@echo "Stopping infrastructure simulation..."
	ansible-playbook -i inventory/hosts.ini playbooks/decommission.yml || true
	docker-compose down
	@echo "Infrastructure simulation stopped"

patch: ## Apply security patches to all nodes
	@echo "Applying security patches..."
	ansible-playbook -i inventory/hosts.ini playbooks/patch.yml
	@echo "Security patches applied"

destroy: ## Completely destroy the infrastructure
	@echo "Destroying infrastructure..."
	ansible-playbook -i inventory/hosts.ini playbooks/decommission.yml || true
	docker-compose down -v --remove-orphans
	docker system prune -f
	rm -rf logs/* reports/*
	@echo "Infrastructure completely destroyed"

# Scaling operations
scale-up-web: ## Scale up web servers (usage: make scale-up-web COUNT=2)
	@echo "Scaling up $(COUNT) web servers..."
	./scripts/simulate_scaling.sh up $(or $(COUNT),1) web

scale-up-db: ## Scale up database servers (usage: make scale-up-db COUNT=1)
	@echo "Scaling up $(COUNT) database servers..."
	./scripts/simulate_scaling.sh up $(or $(COUNT),1) db

scale-down-web: ## Scale down web servers (usage: make scale-down-web COUNT=1)
	@echo "Scaling down $(COUNT) web servers..."
	./scripts/simulate_scaling.sh down $(or $(COUNT),1) web

scale-down-db: ## Scale down database servers (usage: make scale-down-db COUNT=1)
	@echo "Scaling down $(COUNT) database servers..."
	./scripts/simulate_scaling.sh down $(or $(COUNT),1) db

# Failure simulation
fail-network: ## Simulate network failure (usage: make fail-network DURATION=60)
	@echo "Simulating network failure for $(or $(DURATION),60) seconds..."
	./scripts/simulate_failure.sh network $(or $(DURATION),60)

fail-disk: ## Simulate disk space exhaustion (usage: make fail-disk DURATION=120)
	@echo "Simulating disk failure for $(or $(DURATION),120) seconds..."
	./scripts/simulate_failure.sh disk $(or $(DURATION),120)

fail-service: ## Simulate service failures (usage: make fail-service DURATION=30)
	@echo "Simulating service failure for $(or $(DURATION),30) seconds..."
	./scripts/simulate_failure.sh service $(or $(DURATION),30)

fail-node: ## Simulate complete node failure (usage: make fail-node DURATION=300)
	@echo "Simulating node failure for $(or $(DURATION),300) seconds..."
	./scripts/simulate_failure.sh node $(or $(DURATION),300)

# Monitoring and status
status: ## Show infrastructure status
	@echo "=== Docker Containers ==="
	docker-compose ps
	@echo ""
	@echo "=== Ansible Inventory ==="
	ansible -i inventory/hosts.ini --list-hosts all || echo "Inventory check failed"
	@echo ""
	@echo "=== System Resources ==="
	docker stats --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemPerc}}\t{{.NetIO}}"

logs: ## Show infrastructure logs
	docker-compose logs -f --tail=100

logs-web: ## Show web server logs
	docker-compose logs -f web

logs-db: ## Show database logs
	docker-compose logs -f db

# Testing and validation
test: ## Run infrastructure tests
	@echo "Running infrastructure tests..."
	ansible -i inventory/hosts.ini all -m ping
	ansible-playbook -i inventory/hosts.ini --syntax-check playbooks/*.yml
	@echo "Testing scaling scripts..."
	./scripts/simulate_scaling.sh up 0 web  # Dry run
	./scripts/simulate_failure.sh network 1  # Quick test
	@echo "All tests passed"

validate: ## Validate infrastructure configuration
	@echo "Validating configuration..."
	ansible-playbook -i inventory/hosts.ini playbooks/provision.yml --check
	docker-compose config
	@echo "Configuration validation complete"

# Scenarios
scenario-scaling: ## Run scaling event scenario
	@echo "Running scaling event scenario..."
	ansible-playbook -i inventory/hosts.ini scenarios/scaling_event.yml

scenario-disaster: ## Run disaster recovery scenario
	@echo "Running disaster recovery scenario..."
	ansible-playbook -i inventory/hosts.ini scenarios/disaster_recovery.yml

# Maintenance
clean: ## Clean up temporary files and logs
	@echo "Cleaning up temporary files..."
	rm -rf logs/*.log reports/*.txt
	docker system prune -f
	@echo "Cleanup complete"

backup: ## Create infrastructure backup
	@echo "Creating infrastructure backup..."
	mkdir -p backups/$(shell date +%Y%m%d_%H%M%S)
	ansible-playbook -i inventory/hosts.ini playbooks/backup.yml
	docker-compose exec ansible tar -czf /backups/infra_backup.tar.gz /infrastructure
	@echo "Backup created"

# Development
lint: ## Lint Ansible playbooks
	@echo "Linting Ansible playbooks..."
	ansible-lint playbooks/*.yml scenarios/*.yml
	@echo "Linting complete"

format: ## Format code and configuration
	@echo "Formatting code..."
	# Add formatting commands here
	@echo "Formatting complete"

# Security
harden: ## Apply security hardening
	@echo "Applying security hardening..."
	ansible-playbook -i inventory/hosts.ini playbooks/hardening.yml

security-scan: ## Run security scans
	@echo "Running security scans..."
	ansible-playbook -i inventory/hosts.ini playbooks/security_scan.yml

# Help for specific targets
help-scaling: ## Show scaling-related commands
	@echo "Scaling Commands:"
	@echo "  make scale-up-web COUNT=2     - Add 2 web servers"
	@echo "  make scale-up-db COUNT=1      - Add 1 database server"
	@echo "  make scale-down-web COUNT=1   - Remove 1 web server"
	@echo "  make scale-down-db COUNT=1    - Remove 1 database server"

help-failure: ## Show failure simulation commands
	@echo "Failure Simulation Commands:"
	@echo "  make fail-network DURATION=60    - Network failure for 60s"
	@echo "  make fail-disk DURATION=120      - Disk exhaustion for 120s"
	@echo "  make fail-service DURATION=30    - Service failure for 30s"
	@echo "  make fail-node DURATION=300      - Node failure for 300s"
