.PHONY: help install install-frontend install-backend install-cli \
        build run dev migrate setup-env lint test \
        deploy-railway deploy-railway-init deploy-railway-sync-env

# Default target
help:
	@echo "Data Talks - Available commands:"
	@echo ""
	@echo "  make install           Install frontend and backend dependencies"
	@echo "  make install-frontend  Install frontend (npm) dependencies"
	@echo "  make install-backend   Install backend (uv) dependencies"
	@echo "  make install-cli       Install the data-talks CLI via uv"
	@echo "  make build             Build frontend for production"
	@echo "  make run               Build frontend and start backend (http://localhost:8000)"
	@echo "  make dev               Start backend + frontend dev server with hot reload"
	@echo "                          (auto-frees :5173 + :8000-8005 from our own zombies first)"
	@echo "  make migrate           Run database migrations"
	@echo "  make setup-env         Copy backend/.env.example to backend/.env (if not exists)"
	@echo "  make lint              Run frontend linter"
	@echo "  make test              Run frontend tests"
	@echo ""
	@echo "  make deploy-railway          Deploy to Railway (project must be linked)"
	@echo "  make deploy-railway-init     First-time setup: login, link/create project"
	@echo "  make deploy-railway-sync-env Push backend/.env to Railway variables"

# ------------------------------------------------------------------
# Install
# ------------------------------------------------------------------

install: install-frontend install-backend

install-frontend:
	npm install

install-backend:
	cd backend && uv sync

install-cli:
	cd backend && uv sync

# ------------------------------------------------------------------
# Build & Run
# ------------------------------------------------------------------

build:
	npm run build

run: build setup-env
	cd backend && uv run data-talks run

dev: setup-env
	@echo "Starting backend and frontend dev server..."
	@echo "Freeing dev ports (only kills our own uvicorn/data-talks/vite processes)..."
	@./scripts/free-dev-ports.sh
	@(cd backend && uv run data-talks run) &
	@echo "Waiting for backend to start..."
	@for i in 1 2 3 4 5 6 7 8 9 10; do \
		if [ -f backend/.backend_port ]; then break; fi; \
		sleep 1; \
	done
	@BACKEND_PORT=$$(cat backend/.backend_port 2>/dev/null || echo 8000); \
	echo "✅ Backend running on port $$BACKEND_PORT"; \
	echo "🚀 Starting frontend (Vite will proxy /api → backend:$$BACKEND_PORT)..."; \
	npm run dev

# ------------------------------------------------------------------
# Database
# ------------------------------------------------------------------

migrate: setup-env
	cd backend && uv run alembic upgrade head

# ------------------------------------------------------------------
# Utilities
# ------------------------------------------------------------------

setup-env:
	@if [ ! -f backend/.env ]; then \
		cp backend/.env.example backend/.env; \
		echo "Created backend/.env from .env.example — edit it to add your API keys."; \
	fi

lint:
	npm run lint

test:
	npm test

# ------------------------------------------------------------------
# Deploy (Railway)
# ------------------------------------------------------------------

deploy-railway:
	./scripts/deploy-railway.sh

deploy-railway-init:
	./scripts/deploy-railway.sh --init

deploy-railway-sync-env:
	./scripts/deploy-railway.sh --sync-env
