.PHONY: help dev up down build build-no-cache logs logs-bot restart clean migrate shell db-shell frontend-shell backend-shell lint test

DC = sudo docker-compose

# Default target
help:
	@echo "Marathon WebApp - Available commands:"
	@echo ""
	@echo "  Development:"
	@echo "    make dev          - Start all services in development mode"
	@echo "    make up           - Start all services (detached)"
	@echo "    make down         - Stop all services"
	@echo "    make restart      - Restart all services"
	@echo "    make logs         - Show logs (all services)"
	@echo "    make logs-b       - Show backend logs"
	@echo "    make logs-f       - Show frontend logs"
	@echo "    make logs-bot     - Show Telegram bot logs"
	@echo ""
	@echo "  Build:"
	@echo "    make build        - Build all containers (with cache)"
	@echo "    make build-no-cache - Build all containers (no cache)"
	@echo "    make reup         - Rebuild with cache: down + build + up"
	@echo "    make rebuild      - Full rebuild: down + build --no-cache + up"
	@echo "    make rebuild-frontend - Rebuild only frontend"
	@echo "    make rebuild-backend  - Rebuild only backend"
	@echo ""
	@echo "  Database:"
	@echo "    make migrate      - Run database migrations"
	@echo "    make db-shell     - Open PostgreSQL shell"
	@echo ""
	@echo "  Shell access:"
	@echo "    make shell        - Open backend shell"
	@echo "    make frontend-sh  - Open frontend shell"
	@echo ""
	@echo "  Backup:"
	@echo "    make backup-now   - Run backup immediately"
	@echo "    make backup-list  - List available backups in S3"
	@echo "    make backup-restore - Restore from backup (interactive)"
	@echo "    make backup-logs  - Show backup service logs"
	@echo ""
	@echo "  Cleanup:"
	@echo "    make clean        - Stop and remove containers, volumes"
	@echo "    make prune        - Remove unused Docker resources"

# Development
dev:
	$(DC) up

up:
	$(DC) up -d

down:
	$(DC) down

restart:
	$(DC) restart

logs:
	$(DC) logs -f

logs-b:
	$(DC) logs -f backend

logs-f:
	$(DC) logs -f frontend

logs-bot:
	$(DC) logs -f bot

# Build
build:
	$(DC) build

build-no-cache:
	$(DC) build --no-cache

reup:
	$(DC) down
	$(DC) build
	$(DC) up -d

rebuild:
	$(DC) down
	$(DC) build --no-cache
	$(DC) up -d

rebuild-frontend:
	$(DC) down
	sudo docker rmi marathon-frontend || true
	$(DC) build --no-cache frontend
	$(DC) up -d

rebuild-backend:
	$(DC) down
	sudo docker rmi marathon-backend || true
	$(DC) build --no-cache backend
	$(DC) up -d

# Database
migrate:
	$(DC) exec backend alembic upgrade head

migrate-new:
	@read -p "Migration message: " msg; \
	$(DC) exec backend alembic revision --autogenerate -m "$$msg"

db-shell:
	$(DC) exec db psql -U marathon -d marathon

# Shell access
shell:
	$(DC) exec backend bash

backend-shell: shell

frontend-sh:
	$(DC) exec frontend sh

# Cleanup
clean:
	$(DC) down -v --remove-orphans

prune:
	sudo docker system prune -f

# Local development (without Docker)
install:
	cd backend && pip install -r requirements.txt
	cd frontend && npm install

run-backend:
	cd backend && uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

run-frontend:
	cd frontend && npm run dev

# Linting and testing
lint-backend:
	cd backend && ruff check app

lint-frontend:
	cd frontend && npm run lint

test-backend:
	cd backend && pytest

# Production
prod:
	$(DC) -f docker-compose.yml up -d --build

# Backup
backup-now:
	$(DC) exec backup python /app/backup.py

backup-list:
	$(DC) exec backup python /app/restore.py

backup-restore:
	@read -p "Backup filename: " file; \
	$(DC) exec -it backup python /app/restore.py "$$file"

backup-logs:
	$(DC) logs -f backup

backup-shell:
	$(DC) exec backup bash
