.PHONY: help setup start stop restart logs clean build test init-db

# Colors for terminal output
GREEN  := \033[0;32m
YELLOW := \033[0;33m
NC     := \033[0m # No Color

help: ## Show this help message
	@echo '${GREEN}Family Hub - Available Commands${NC}'
	@echo ''
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  ${YELLOW}%-15s${NC} %s\n", $$1, $$2}'
	@echo ''

setup: ## Initial setup (copy .env.example to .env)
	@echo "${GREEN}Setting up Family Hub...${NC}"
	@if [ ! -f .env ]; then \
		cp .env.example .env; \
		echo "${YELLOW}Created .env file - please edit it with your SECRET_KEY${NC}"; \
	else \
		echo "${YELLOW}.env file already exists${NC}"; \
	fi
	@if [ ! -f backend/.env ]; then \
		cp backend/.env.example backend/.env; \
		echo "${YELLOW}Created backend/.env file${NC}"; \
	fi
	@echo "${GREEN}Setup complete!${NC}"

start: ## Start all services
	@echo "${GREEN}Starting Family Hub...${NC}"
	docker-compose up -d
	@echo "${GREEN}Services started!${NC}"
	@echo "Frontend: http://localhost:5173"
	@echo "Backend:  http://localhost:8000"
	@echo "API Docs: http://localhost:8000/docs"

stop: ## Stop all services
	@echo "${YELLOW}Stopping Family Hub...${NC}"
	docker-compose down
	@echo "${GREEN}Services stopped${NC}"

restart: ## Restart all services
	@echo "${YELLOW}Restarting Family Hub...${NC}"
	docker-compose restart
	@echo "${GREEN}Services restarted${NC}"

build: ## Rebuild containers
	@echo "${GREEN}Building containers...${NC}"
	docker-compose build
	@echo "${GREEN}Build complete${NC}"

rebuild: ## Rebuild and restart containers
	@echo "${GREEN}Rebuilding containers...${NC}"
	docker-compose up -d --build
	@echo "${GREEN}Containers rebuilt and started${NC}"

logs: ## View logs (follow mode)
	docker-compose logs -f

logs-backend: ## View backend logs
	docker-compose logs -f backend

logs-frontend: ## View frontend logs
	docker-compose logs -f frontend

clean: ## Stop and remove all containers, volumes, and images
	@echo "${YELLOW}Cleaning up Family Hub...${NC}"
	docker-compose down -v
	@echo "${GREEN}Cleanup complete${NC}"

init-db: ## Initialize the database with family members
	@echo "${GREEN}Initializing database...${NC}"
	docker-compose exec backend python init_db.py
	@echo "${GREEN}Database initialized with family member accounts${NC}"
	@echo "${YELLOW}Default credentials: username=jess, password=changeme123${NC}"

shell-backend: ## Open shell in backend container
	docker-compose exec backend bash

shell-frontend: ## Open shell in frontend container
	docker-compose exec frontend sh

test: ## Run tests
	@echo "${GREEN}Running backend tests...${NC}"
	docker-compose exec backend pytest
	@echo "${GREEN}Running frontend tests...${NC}"
	docker-compose exec frontend npm test

status: ## Show container status
	docker-compose ps

dev-backend: ## Run backend locally (outside Docker)
	cd backend && uvicorn app.main:app --reload

dev-frontend: ## Run frontend locally (outside Docker)
	cd frontend && npm run dev

install-backend: ## Install backend dependencies locally
	cd backend && pip install -r requirements.txt

install-frontend: ## Install frontend dependencies locally
	cd frontend && npm install
