# Colors for output
GREEN := $(shell printf '\033[32m')
YELLOW := $(shell printf '\033[33m')
NC := $(shell printf '\033[0m')  # No Color

# ─── Environment targeting (ENV=local|dev|prod) ───────────────────────────
# Usage: make <target> ENV=dev  or  make <target> ENV=prod
ENV ?= local
ifeq ($(ENV),dev)
ENV_CMD = set -a && . .env.dev && set +a &&
else ifeq ($(ENV),prod)
ENV_CMD = set -a && . .env.prod && set +a &&
else
ENV_CMD =
endif

run:
	PYTHONPATH=. python3 main.py

run-dev:
	@echo "$(YELLOW)Running data ingestion against DEV database...$(NC)"
	set -a && . .env.dev && set +a && PYTHONPATH=. python3 main.py

run-prod:
	@echo "$(YELLOW)Running data ingestion against PROD database...$(NC)"
	set -a && . .env.prod && set +a && PYTHONPATH=. python3 main.py

sync-series-info:
	PYTHONPATH=. python3 processors/sync_series_info.py

sync-series-info-dev:
	@echo "$(YELLOW)Syncing series info against DEV database...$(NC)"
	set -a && . .env.dev && set +a && PYTHONPATH=. python3 processors/sync_series_info.py

sync-series-info-prod:
	@echo "$(YELLOW)Syncing series info against PROD database...$(NC)"
	set -a && . .env.prod && set +a && PYTHONPATH=. python3 processors/sync_series_info.py

test:
	@echo "$(YELLOW)Running tests...$(NC)"
	PYTHONPATH=. venv/bin/python -m pytest tests/ -v
	@echo "$(GREEN)Tests completed.$(NC)"

logs:
	tail -n 20 logs/balyonline.log

clean:
	find . -name '*.pyc' -delete
	find . -name '__pycache__' -type d -exec rm -r {} +

clean-untracked:
	@echo "$(YELLOW)Removing all untracked files in git...$(NC)"
	git clean -fd

# Create all database tables
database-create:
	@echo "Creating database tables..."
	- PYTHONPATH=. python -c "from db.database import create_tables; create_tables()"

# Database migration commands
migrate-stamp:
	@echo "$(YELLOW)Marking current database state as baseline...$(NC)"
	@alembic stamp head
	@echo "$(GREEN)Database stamped successfully$(NC)"

migrate-create:
	@if [ -z "$(msg)" ]; then \
		echo "$(YELLOW)Error: Please provide a migration message$(NC)"; \
		echo "Usage: make migrate-create msg=\"your message here\""; \
		exit 1; \
	fi
	@echo "$(YELLOW)Creating migration: $(msg)$(NC)"
	@alembic revision --autogenerate -m "$(msg)"
	@echo "$(GREEN)Migration created successfully$(NC)"

migrate-upgrade:
	@echo "$(YELLOW)Applying migrations...$(NC)"
	@alembic upgrade head
	@echo "$(GREEN)Migrations applied successfully$(NC)"

migrate-dev:
	@echo "$(YELLOW)Migrating DEV database...$(NC)"
	set -a && . .env.dev && set +a && alembic upgrade head
	@echo "$(GREEN)DEV migrations applied successfully$(NC)"

migrate-prod:
	@echo "$(YELLOW)Migrating PROD database...$(NC)"
	set -a && . .env.prod && set +a && alembic upgrade head
	@echo "$(GREEN)PROD migrations applied successfully$(NC)"

migrate-downgrade:
	@echo "$(YELLOW)Rolling back last migration...$(NC)"
	@alembic downgrade -1
	@echo "$(GREEN)Migration rolled back successfully$(NC)"

migrate-current:
	@echo "$(YELLOW)Current database revision:$(NC)"
	@alembic current

migrate-history:
	@echo "$(YELLOW)Migration history:$(NC)"
	@alembic history --verbose

migrate-help:
	@echo "$(GREEN)Database Migration Commands:$(NC)"
	@echo "  make migrate-stamp          - Mark current DB state as baseline (first time only)"
	@echo "  make migrate-create msg=\"...\" - Create a new migration"
	@echo "  make migrate-upgrade        - Apply all pending migrations"
	@echo "  make migrate-dev            - Apply migrations to DEV database"
	@echo "  make migrate-prod           - Apply migrations to PROD database"
	@echo "  make migrate-downgrade      - Rollback last migration"
	@echo "  make migrate-current        - Show current revision"
	@echo "  make migrate-history        - Show migration history"
	@echo ""
	@echo "$(YELLOW)Example workflow:$(NC)"
	@echo "  1. make migrate-stamp                          # First time only"
	@echo "  2. make migrate-create msg=\"add fred tables\"   # Create migration"
	@echo "  3. make migrate-upgrade                        # Apply local migration"
	@echo "  4. make migrate-prod                           # Apply migration to PROD"

# ─── IPUMS Interactive Shell ──────────────────────────────────────────────
ipums-shell:
	@echo "$(YELLOW)Launching DuckDB shell with IPUMS data loaded as 'census'...$(NC)"
	PYTHONPATH=. python processors/ipums/shell.py
	@echo "$(GREEN)Shell closed.$(NC)"

# ─── IPUMS Hypothesis Scripts (on-demand, not part of daily pipeline) ─────
ipums-all: ipums-overall-bachelors-unemployment ipums-cs-bachelors-unemployment
	@echo "$(GREEN)All IPUMS hypothesis scripts completed successfully.$(NC)"

ipums-overall-bachelors-unemployment:
	@echo "$(YELLOW)Running IPUMS hypothesis: Overall Bachelor's Unemployment Rate...$(NC)"
	$(ENV_CMD) PYTHONPATH=. python processors/ipums/hypotheses/overall_bachelors_unemployment.py
	@echo "$(GREEN)Done.$(NC)"

ipums-cs-bachelors-unemployment:
	@echo "$(YELLOW)Running IPUMS hypothesis: CS Bachelor's Unemployment Rate...$(NC)"
	$(ENV_CMD) PYTHONPATH=. python processors/ipums/hypotheses/cs_bachelors_unemployment.py
	@echo "$(GREEN)Done.$(NC)"

# ipums-cs-senior-bachelors-unemployment:
# 	@echo "$(YELLOW)Running IPUMS hypothesis: Senior CS Bachelor's Unemployment Rate (35-50)...$(NC)"
# 	$(ENV_CMD) PYTHONPATH=. python processors/ipums/hypotheses/cs_senior_bachelors_unemployment.py
# 	@echo "$(GREEN)Done.$(NC)"

# ipums-nursing-bachelors-unemployment:
# 	@echo "$(YELLOW)Running IPUMS hypothesis: Nursing Bachelor's Unemployment Rate...$(NC)"
# 	$(ENV_CMD) PYTHONPATH=. python processors/ipums/hypotheses/nursing_bachelors_unemployment.py
# 	@echo "$(GREEN)Done.$(NC)"

# ipums-bachelors-demographics:
# 	@echo "$(YELLOW)Displaying 2024 Bachelor's Degree Demographics...$(NC)"
# 	$(ENV_CMD) PYTHONPATH=. python processors/ipums/hypotheses/demographics_bachelors.py
# 	@echo "$(GREEN)Done.$(NC)"

# ipums-business-admin-bachelors-unemployment:
# 	@echo "$(YELLOW)Running IPUMS hypothesis: Business Admin Bachelor's Unemployment Rate...$(NC)"
# 	$(ENV_CMD) PYTHONPATH=. python processors/ipums/hypotheses/business_admin_bachelors_unemployment.py
# 	@echo "$(GREEN)Done.$(NC)"

# ipums-accounting-bachelors-unemployment:
# 	@echo "$(YELLOW)Running IPUMS hypothesis: Accounting Bachelor's Unemployment Rate...$(NC)"
# 	$(ENV_CMD) PYTHONPATH=. python processors/ipums/hypotheses/accounting_bachelors_unemployment.py
# 	@echo "$(GREEN)Done.$(NC)"

# ipums-marketing-bachelors-unemployment:
# 	@echo "$(YELLOW)Running IPUMS hypothesis: Marketing Bachelor's Unemployment Rate...$(NC)"
# 	$(ENV_CMD) PYTHONPATH=. python processors/ipums/hypotheses/marketing_bachelors_unemployment.py
# 	@echo "$(GREEN)Done.$(NC)"

# ipums-finance-bachelors-unemployment:
# 	@echo "$(YELLOW)Running IPUMS hypothesis: Finance Bachelor's Unemployment Rate...$(NC)"
# 	$(ENV_CMD) PYTHONPATH=. python processors/ipums/hypotheses/finance_bachelors_unemployment.py
# 	@echo "$(GREEN)Done.$(NC)"

# ipums-tech-headcount:
# 	@echo "$(YELLOW)Running IPUMS hypothesis: Routine vs Complex Tech Headcount...$(NC)"
# 	$(ENV_CMD) PYTHONPATH=. python processors/ipums/hypotheses/routine_vs_complex_tech_headcount.py
# 	@echo "$(GREEN)Done.$(NC)"

# ipums-occ-headcount-breakdown:
# 	@echo "$(YELLOW)Running IPUMS hypothesis: Routine vs Complex OCC Breakdown...$(NC)"
# 	$(ENV_CMD) PYTHONPATH=. python processors/ipums/hypotheses/routine_vs_complex_occ_breakdown.py
# 	@echo "$(GREEN)Done.$(NC)"

# ipums-software-dev-age-buckets:
# 	@echo "$(YELLOW)Running IPUMS hypothesis: Software Developer Age Buckets...$(NC)"
# 	$(ENV_CMD) PYTHONPATH=. python processors/ipums/hypotheses/software_dev_age_buckets.py
# 	@echo "$(GREEN)Done.$(NC)"

# ipums-software-dev-age-stats:
# 	@echo "$(YELLOW)Running IPUMS hypothesis: Software Developer Age Stats...$(NC)"
# 	$(ENV_CMD) PYTHONPATH=. python processors/ipums/hypotheses/software_dev_age_stats.py
# 	@echo "$(GREEN)Done.$(NC)"

# ipums-cs-bachelors-employment:
# 	@echo "$(YELLOW)Running IPUMS hypothesis: CS Bachelors Employment Status...$(NC)"
# 	$(ENV_CMD) PYTHONPATH=. python processors/ipums/hypotheses/cs_bachelors_employment_status.py
# 	@echo "$(GREEN)Done.$(NC)"

# ipums-cs-bachelors-software-dev-employment:
# 	@echo "$(YELLOW)Running IPUMS hypothesis: CS Bachelors SWDev Employment Status...$(NC)"
# 	$(ENV_CMD) PYTHONPATH=. python processors/ipums/hypotheses/cs_bachelors_software_dev_employment.py
# 	@echo "$(GREEN)Done.$(NC)"

# ipums-cs-juniors-absorbed-tech-headcount:
# 	@echo "$(YELLOW)Running IPUMS hypothesis: CS Juniors Absorbed Tech Headcount...$(NC)"
# 	$(ENV_CMD) PYTHONPATH=. python processors/ipums/hypotheses/ipums_cs_juniors_absorbed_tech_headcount.py
# 	@echo "$(GREEN)Done.$(NC)"

# ipums-cs-juniors-spillover-headcount:
# 	@echo "$(YELLOW)Running IPUMS hypothesis: CS Juniors Spillover (Non-Tech) Headcount...$(NC)"
# 	$(ENV_CMD) PYTHONPATH=. python processors/ipums/hypotheses/ipums_cs_juniors_spillover_headcount.py
# 	@echo "$(GREEN)Done.$(NC)"

# ipums-cs-juniors-unemployed:
# 	@echo "$(YELLOW)Running IPUMS hypothesis: CS Juniors Unemployed Headcount...$(NC)"
# 	$(ENV_CMD) PYTHONPATH=. python processors/ipums/hypotheses/ipums_cs_juniors_unemployed.py
# 	@echo "$(GREEN)Done.$(NC)"

# ipums-cs-juniors-supply:
# 	@echo "$(YELLOW)Running IPUMS hypothesis: CS Juniors Total Supply...$(NC)"
# 	$(ENV_CMD) PYTHONPATH=. python processors/ipums/hypotheses/ipums_cs_juniors_supply.py
# 	@echo "$(GREEN)Done.$(NC)"

# ─── Database Truncation ─────────────────────────────────────────────────
truncate:
	@echo "$(YELLOW)Truncating ALL tables in LOCAL database...$(NC)"
	@read -p "Are you sure? [y/N] " confirm && [ "$$confirm" = "y" ] || (echo "Aborted." && exit 1)
	PYTHONPATH=. python -m db.truncate
	@echo "$(GREEN)Local database truncated.$(NC)"

truncate-dev:
	@echo "$(YELLOW)Truncating ALL tables in DEV database...$(NC)"
	@read -p "Are you sure? [y/N] " confirm && [ "$$confirm" = "y" ] || (echo "Aborted." && exit 1)
	set -a && . .env.dev && set +a && PYTHONPATH=. python -m db.truncate
	@echo "$(GREEN)DEV database truncated.$(NC)"

truncate-prod:
	@echo "$(YELLOW)Truncating ALL tables in PROD database...$(NC)"
	@read -p "Type 'yes-prod' to confirm: " confirm && [ "$$confirm" = "yes-prod" ] || (echo "Aborted." && exit 1)
	set -a && . .env.prod && set +a && PYTHONPATH=. python -m db.truncate
	@echo "$(GREEN)PROD database truncated.$(NC)"

.PHONY: run sync-series-info sync-series-info-dev sync-series-info-prod test logs clean database-create truncate truncate-dev truncate-prod migrate-stamp migrate-create migrate-upgrade migrate-downgrade migrate-current migrate-history migrate-help ipums-shell ipums-all ipums-overall-bachelors-unemployment ipums-cs-bachelors-unemployment ipums-cs-senior-bachelors-unemployment ipums-nursing-bachelors-unemployment ipums-bachelors-demographics ipums-business-admin-bachelors-unemployment ipums-accounting-bachelors-unemployment ipums-marketing-bachelors-unemployment ipums-finance-bachelors-unemployment ipums-tech-headcount ipums-occ-headcount-breakdown ipums-software-dev-age-buckets ipums-software-dev-age-stats ipums-cs-bachelors-employment ipums-cs-bachelors-software-dev-employment