SHELL := /bin/bash

.PHONY: help deploy deploy-all deploy-infra deploy-k8s deploy-blockscout deploy-contracts deploy-dns deploy-weth deploy-weth10 deploy-weth-ccip deploy-ccip-weth9-bridge deploy-ccip-weth10-bridge keyvaults keyvaults-status keyvaults-permissions keyvaults-store-keys keyvaults-complete azure-list-resources azure-check-naming verify test clean assets

help:
	@echo "DeFi Oracle Meta Mainnet - Makefile"
	@echo ""
	@echo "Available targets:"
	@echo "  make deploy          - Deploy infrastructure and applications (legacy)"
	@echo "  make deploy-all      - Deploy everything (infrastructure, k8s, contracts, DNS)"
	@echo "  make deploy-infra    - Deploy Azure infrastructure only"
	@echo "  make deploy-k8s      - Deploy Kubernetes resources only"
	@echo "  make deploy-blockscout - Deploy Blockscout explorer only"
	@echo "  make deploy-contracts - Deploy contracts only"
	@echo "  make deploy-dns      - Configure Cloudflare DNS only"
	@echo "  make deploy-weth     - Deploy WETH9 contract only"
	@echo "  make deploy-weth10   - Deploy WETH10 contract only"
	@echo "  make deploy-weth-ccip - Deploy all WETH contracts and CCIP bridges"
	@echo "  make deploy-ccip-weth9-bridge - Deploy CCIPWETH9Bridge only"
	@echo "  make deploy-ccip-weth10-bridge - Deploy CCIPWETH10Bridge only"
	@echo "  make keyvaults        - Deploy all Key Vaults"
	@echo "  make keyvaults-status - Check Key Vault deployment status"
	@echo "  make keyvaults-permissions - Grant Key Vault permissions"
	@echo "  make keyvaults-store-keys - Store validator keys in Key Vaults"
	@echo "  make keyvaults-complete - Run complete Key Vault setup"
	@echo "  make azure-list-resources - List all Azure resources"
	@echo "  make azure-check-naming - Check Azure resource naming conventions"
	@echo "  make monitor         - Monitor deployment status (consolidated)"
	@echo "  make monitor-continuous - Continuous deployment monitoring"
	@echo "  make monitor-dashboard - Deployment dashboard view"
	@echo "  make deploy-parallel - Parallel deployment (infrastructure)"
	@echo "  make calculate-costs - Calculate deployment costs"
	@echo "  make verify          - Verify deployment"
	@echo "  make azure-login     - Authenticate with Azure CLI (required for deployment)"
	@echo "  make test            - Run tests"
	@echo "  make clean           - Clean up temporary files"
	@echo "  make genesis         - Generate genesis file"
	@echo "  make keys            - Generate validator and oracle keys"
	@echo "  make contracts       - Compile and test contracts (optional: SCOPE=treasury, SCOPE=bridge/trustless, ...)"
	@echo "  make monitoring      - Deploy monitoring stack"
	@echo "  make assets          - Setup and download Azure icons (see Makefile.assets)"

.PHONY: help validate docs index measure all

help:
	@echo "Available targets:"
	@echo "  validate  - Run script QA (bash -n, shellcheck if available)"
	@echo "  docs      - Generate per-script docs under docs/scripts/"
	@echo "  index     - Generate COMMANDS_INDEX.md and SCRIPTS_INDEX.md + tags"
	@echo "  measure   - Measure library init startup time"
	@echo "  all       - validate, docs, index"

validate:
	bash scripts/automation/validate-scripts.sh

docs:
	bash scripts/automation/generate-script-docs.sh

index:
	bash scripts/automation/generate-commands-index.sh
	bash scripts/automation/generate-docs-index.sh

measure:
	bash scripts/automation/measure-startup.sh

all: validate docs index

deploy:
	@echo "Deploying infrastructure..."
	cd terraform && terraform init && terraform apply
	@echo "Deploying Kubernetes resources..."
	kubectl apply -k k8s/base
	@echo "Deployment complete!"

deploy-all:
	@echo "Deploying everything..."
	./scripts/deployment/deploy-all.sh

deploy-infra:
	@echo "Deploying infrastructure only..."
	./scripts/deployment/deploy-all.sh --skip-kubernetes --skip-blockscout --skip-contracts --skip-cloudflare

deploy-k8s:
	@echo "Deploying Kubernetes resources only..."
	./scripts/deployment/deploy-all.sh --skip-infrastructure --skip-blockscout --skip-contracts --skip-cloudflare

deploy-blockscout:
	@echo "Deploying Blockscout only..."
	./scripts/deployment/deploy-all.sh --skip-infrastructure --skip-kubernetes --skip-contracts --skip-cloudflare

deploy-contracts:
	@echo "Deploying contracts only (parallel)..."
	@if [ ! -f .env ]; then \
		echo "Error: .env file not found. Please create .env file first."; \
		exit 1; \
	fi
	@source .env && ./scripts/deployment/deploy-contracts-parallel.sh

deploy-dns:
	@echo "Configuring Cloudflare DNS..."
	@echo "Usage: make deploy-dns ZONE_ID=<zone_id> API_TOKEN=<token> IP=<ip_address>"
	@if [ -z "$(ZONE_ID)" ] || [ -z "$(API_TOKEN)" ] || [ -z "$(IP)" ]; then \
		echo "Error: ZONE_ID, API_TOKEN, and IP are required"; \
		exit 1; \
	fi
	./scripts/deployment/cloudflare-dns.sh --zone-id $(ZONE_ID) --api-token $(API_TOKEN) --ip $(IP)

deploy-weth:
	@echo "Deploying WETH9 contract..."
	./scripts/deployment/deploy-weth.sh

deploy-weth10:
	@echo "Deploying WETH10 contract..."
	./scripts/deployment/deploy-weth10.sh

deploy-weth-ccip:
	@echo "Deploying all WETH contracts and CCIP bridges..."
	./scripts/deployment/deploy-weth-with-ccip.sh

deploy-ccip-weth9-bridge:
	@echo "Deploying CCIPWETH9Bridge..."
	./scripts/deployment/deploy-ccip-weth9-bridge.sh

deploy-ccip-weth10-bridge:
	@echo "Deploying CCIPWETH10Bridge..."
	./scripts/deployment/deploy-ccip-weth10-bridge.sh

keyvaults:
	@echo "Deploying Key Vaults..."
	./scripts/key-management/manage-keyvaults.sh deploy

keyvaults-status:
	@echo "Checking Key Vault status..."
	./scripts/key-management/manage-keyvaults.sh status

keyvaults-permissions:
	@echo "Granting Key Vault permissions..."
	./scripts/key-management/manage-keyvaults.sh permissions

keyvaults-store-keys:
	@echo "Storing validator keys in Key Vaults..."
	./scripts/key-management/manage-keyvaults.sh store-keys

keyvaults-complete:
	@echo "Running complete Key Vault setup..."
	./scripts/key-management/manage-keyvaults.sh complete

azure-list-resources:
	@echo "Listing all Azure resources..."
	./scripts/azure/list-all-resources.sh

azure-check-naming:
	@echo "Checking Azure resource naming conventions..."
	./scripts/azure/check-naming-conventions.sh

monitor:
	@echo "Monitoring deployment status..."
	./scripts/deployment/monitor-deployment-consolidated.sh --mode status

monitor-continuous:
	@echo "Starting continuous monitoring..."
	./scripts/deployment/monitor-deployment-consolidated.sh --mode continuous

monitor-live:
	@echo "Starting live monitoring..."
	./scripts/deployment/monitor-deployment-consolidated.sh --mode live

monitor-dashboard:
	@echo "Showing deployment dashboard..."
	./scripts/deployment/monitor-deployment-consolidated.sh --mode dashboard

deploy-parallel:
	@echo "Deploying infrastructure in parallel..."
	./scripts/deployment/deploy-parallel-consolidated.sh --resource infrastructure

deploy-parallel-besu:
	@echo "Deploying Besu network in parallel..."
	./scripts/deployment/deploy-parallel-consolidated.sh --resource besu

deploy-parallel-kubernetes:
	@echo "Configuring Kubernetes in parallel..."
	./scripts/deployment/deploy-parallel-consolidated.sh --resource kubernetes

calculate-costs:
	@echo "Calculating deployment costs..."
	./scripts/deployment/calculate-costs-consolidated.sh

calculate-costs-json:
	@echo "Calculating deployment costs (JSON output)..."
	./scripts/deployment/calculate-costs-consolidated.sh --format json

verify:
	@echo "Verifying deployment (parallel)..."
	@if [ ! -f .env ]; then \
		echo "Error: .env file not found. Please create .env file first."; \
		exit 1; \
	fi
	@source .env && ./scripts/deployment/verify-contracts-parallel.sh

azure-login:
	@echo "Authenticating with Azure CLI..."
	@echo "For WSL users, this will open a browser window for authentication"
	./scripts/deployment/azure-login.sh interactive

test:
	@echo "Running tests (parallel)..."
	@if [ -n "$(SCOPE)" ]; then echo "Forge scope: $(SCOPE)"; else echo "Forge scope: full"; fi
	@if [ -f .env ]; then \
		source .env && FORGE_SCOPE="$(SCOPE)" bash scripts/forge/scope.sh test --fork-url "$$RPC_URL" -j $$(nproc) || FORGE_SCOPE="$(SCOPE)" bash scripts/forge/scope.sh test -j $$(nproc); \
	else \
		FORGE_SCOPE="$(SCOPE)" bash scripts/forge/scope.sh test -j $$(nproc); \
	fi
	@./tests/health-check.sh &
	@HEALTH_PID=$$!; \
	./tests/load-test.sh &
	@LOAD_PID=$$!; \
	wait $$HEALTH_PID $$LOAD_PID

clean:
	@echo "Cleaning up..."
	rm -rf out/
	rm -rf cache/
	rm -rf .terraform/
	rm -rf *.tfstate*

genesis:
	@echo "Generating genesis file..."
	./scripts/generate-genesis-proper.sh 4
	./scripts/validation/validate-genesis.sh

keys:
	@echo "Generating keys..."
	./scripts/key-management/generate-validator-keys.sh
	./scripts/key-management/generate-oracle-keys.sh

contracts:
	@echo "Compiling contracts..."
	@if [ -n "$(SCOPE)" ]; then echo "Forge scope: $(SCOPE)"; else echo "Forge scope: full"; fi
	FORGE_SCOPE="$(SCOPE)" bash scripts/forge/scope.sh build
	@echo "Running contract tests (parallel)..."
	@if [ -f .env ]; then \
		source .env && FORGE_SCOPE="$(SCOPE)" bash scripts/forge/scope.sh test --fork-url "$$RPC_URL" -j $$(nproc) || FORGE_SCOPE="$(SCOPE)" bash scripts/forge/scope.sh test -j $$(nproc); \
	else \
		FORGE_SCOPE="$(SCOPE)" bash scripts/forge/scope.sh test -j $$(nproc); \
	fi

monitoring:
	@echo "Deploying monitoring stack..."
	kubectl apply -f monitoring/k8s/prometheus.yaml
