# === CLAUDE DASHBOARD API MAKEFILE === #
# Main Makefile that includes all sub-makefiles for better organization
#
# Project Structure:
# - makefiles/dev.mk       - Installation, setup, code quality (fmt, lint, type-check)
# - makefiles/usage.mk       - Usage App (API usage and cost analysis)
# - makefiles/analytics.mk   - Analytics App (Claude Code metrics)
# - makefiles/cost-report.mk - Cost Report App (workspace-level costs)
# - makefiles/debug.mk       - Debug tools
# - makefiles/frontend.mk    - Frontend data sync

.PHONY: help all update

# === SHARED VARIABLES === #
# These are used across all sub-makefiles

OUTPUT_DIR = output
FRONTEND_DATA_DIR = ../frontend/src/data

# === HELP === #

help: ## Show this help message
	@echo "Claude Dashboard API - Available Commands"
	@echo ""
	@echo "Main:"
	@echo "  update                         Fetch all data, regenerate, sync, upload to S3 (interactive)"
	@echo ""
	@echo "Setup & Development:"
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' makefiles/dev.mk | \
		awk 'BEGIN {FS = ":.*?## "}; {printf "  %-30s %s\n", $$1, $$2}'

	@echo ""
	@echo "Usage App (API usage & deduced costs):"
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' makefiles/usage.mk | \
		awk 'BEGIN {FS = ":.*?## "}; {printf "  %-30s %s\n", $$1, $$2}'
	@echo ""
	@echo "Analytics App (Claude Code metrics):"
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' makefiles/analytics.mk | \
		awk 'BEGIN {FS = ":.*?## "}; {printf "  %-30s %s\n", $$1, $$2}'
	@echo ""
	@echo "Cost Report App (workspace-level):"
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' makefiles/cost-report.mk | \
		awk 'BEGIN {FS = ":.*?## "}; {printf "  %-30s %s\n", $$1, $$2}'
	@echo ""
	@echo "Frontend Sync:"
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' makefiles/frontend.mk | \
		awk 'BEGIN {FS = ":.*?## "}; {printf "  %-30s %s\n", $$1, $$2}'
	@echo ""
	@echo "Debug Tools:"
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' makefiles/debug.mk | \
		awk 'BEGIN {FS = ":.*?## "}; {printf "  %-30s %s\n", $$1, $$2}'

# === INCLUDE SUB-MAKEFILES === #

include makefiles/dev.mk
include makefiles/usage.mk
include makefiles/analytics.mk
include makefiles/cost-report.mk
include makefiles/debug.mk
include makefiles/frontend.mk

# === DEFAULT TARGET === #

all: install-dev format lint type-check ## Install dependencies, format, lint, and type-check

update: ## Fetch all data sources, regenerate, sync to frontend, and upload to S3
	@read -p "How many days back to fetch? " DAYS; \
	if [ -z "$$DAYS" ] || ! echo "$$DAYS" | grep -qE '^[0-9]+$$'; then \
		echo "❌ Error: Please enter a valid number!"; \
		exit 1; \
	fi; \
	TODAY=$$(date -u +%Y-%m-%d); \
	START=$$(date -u -v-$${DAYS}d -j -f %Y-%m-%d "$$TODAY" +%Y-%m-%d 2>/dev/null || date -u -d "$$TODAY - $${DAYS} days" +%Y-%m-%d); \
	echo ""; \
	echo "=== Updating data: $$START → $$TODAY ($$DAYS days) ==="; \
	echo ""; \
	echo "━━━ [1/7] Fetching usage data..."; \
	mkdir -p $(OUTPUT_DIR)/usage/raw; \
	uv run python -m src.usage.cost_analyzer \
		--start $$START \
		--end $$TODAY \
		--time-grouping day \
		--debug; \
	echo "✅ Usage data fetched!"; \
	echo ""; \
	echo "━━━ [2/7] Fetching analytics data..."; \
	mkdir -p $(OUTPUT_DIR)/analytics/raw; \
	uv run python -m src.analytics.analytics_analyzer \
		--start $$START \
		--end $$TODAY \
		--time-grouping day \
		--debug; \
	echo "✅ Analytics data fetched!"; \
	echo ""; \
	echo "━━━ [3/7] Fetching cost report data..."; \
	mkdir -p $(OUTPUT_DIR)/cost_report/raw; \
	uv run python -m src.cost_report.cost_report_analyzer \
		--start $$START \
		--end $$TODAY \
		--output $(OUTPUT_DIR)/cost_report/cost_report.json \
		--debug; \
	echo "✅ Cost report data fetched!"; \
	echo ""; \
	echo "━━━ [4/7] Regenerating costs.json..."; \
	$(MAKE) regenerate-usage; \
	echo ""; \
	echo "━━━ [5/7] Regenerating analytics.json..."; \
	$(MAKE) regenerate-analytics; \
	echo ""; \
	echo "━━━ [6/7] Regenerating cost_report.json and comparing costs..."; \
	$(MAKE) regenerate-cost-report; \
	$(MAKE) compare-costs; \
	echo ""; \
	echo "━━━ [7/7] Syncing to frontend and uploading to S3..."; \
	$(MAKE) sync-frontend; \
	$(MAKE) upload-s3; \
	echo ""; \
	echo "🎉 All done! Data updated from $$START to $$TODAY."