.PHONY: help install dev start stop logs clean build up down restart

# Project settings
PROJECT_NAME=pg-admin
DOCKER_COMPOSE=docker-compose

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

help:
	@echo "$(BLUE)=== PG-Admin - PostgreSQL Admin Panel ===$(NC)"
	@echo "$(GREEN)Available commands:$(NC)"
	@echo "  make install     - Install dependencies"
	@echo "  make dev         - Run development server"
	@echo "  make start       - Start application"
	@echo "  make stop        - Stop application"
	@echo "  make restart     - Restart application"
	@echo "  make build       - Build Docker image"
	@echo "  make up          - Start Docker containers"
	@echo "  make down        - Stop Docker containers"
	@echo "  make logs        - View Docker logs"
	@echo "  make clean       - Remove containers and volumes"
	@echo "  make db-reset    - Reset database"
	@echo ""

# Install dependencies
install:
	@echo "$(BLUE)Installing dependencies...$(NC)"
	npm install
	@echo "$(GREEN)✓ Dependencies installed$(NC)"

# Development server with hot reload
dev:
	@echo "$(BLUE)Starting development server...$(NC)"
	npm run dev

# Start production server
start:
	@echo "$(BLUE)Starting server...$(NC)"
	npm start

# Stop application
stop:
	@echo "$(BLUE)Stopping server...$(NC)"
	@pkill -f "node server.js" || true

# Restart application
restart: stop start
	@echo "$(GREEN)✓ Server restarted$(NC)"

# Docker - Build image
build:
	@echo "$(BLUE)Building Docker image...$(NC)"
	$(DOCKER_COMPOSE) build
	@echo "$(GREEN)✓ Image built$(NC)"

# Docker - Start containers
up: build
	@echo "$(BLUE)Starting containers...$(NC)"
	$(DOCKER_COMPOSE) up -d
	@echo "$(GREEN)✓ Containers started$(NC)"
	@echo "Application available at http://localhost:3000"

# Docker - Stop containers
down:
	@echo "$(BLUE)Stopping containers...$(NC)"
	$(DOCKER_COMPOSE) down
	@echo "$(GREEN)✓ Containers stopped$(NC)"

# Docker - View logs
logs:
	@echo "$(BLUE)Fetching logs...$(NC)"
	$(DOCKER_COMPOSE) logs -f

# Docker - Clean (remove containers and volumes)
clean:
	@echo "$(YELLOW)Cleaning Docker resources...$(NC)"
	$(DOCKER_COMPOSE) down -v
	@echo "$(GREEN)✓ Cleaned$(NC)"

# Database - Reset database
db-reset:
	@echo "$(YELLOW)Resetting database...$(NC)"
	$(DOCKER_COMPOSE) exec postgres psql -U postgres -d postgres -c "DROP DATABASE IF EXISTS postgres;"
	$(DOCKER_COMPOSE) exec postgres psql -U postgres -d postgres -c "CREATE DATABASE postgres;"
	@echo "$(GREEN)✓ Database reset$(NC)"

# Show all targets
.DEFAULT_GOAL := help

# Перезапуск
restart:
	$(COMPOSE) down
	$(COMPOSE) up -d

# Пересборка + запуск
rebuild:
	$(COMPOSE) up -d --build

# === Логи ===

# Все логи
logs:
	$(COMPOSE) logs -f

# Логи backend
logs-app:
	$(COMPOSE) logs -f backend

# Логи базы
logs-db:
	$(COMPOSE) logs -f postgres

# === Обслуживание ===

# Зайти в контейнер backend
bash:
	$(COMPOSE) exec backend sh

# Зайти в postgres
psql:
	$(COMPOSE) exec postgres psql -U postgres -d testdb

# Очистка (осторожно — удаляет данные!)
clean:
	$(COMPOSE) down -v
	docker system prune -f

# Полный ресет (жёстко)
reset:
	$(COMPOSE) down -v --remove-orphans
	docker system prune -af

# === Обновление ===

# Обновить код + пересобрать
update:
	git pull
	$(COMPOSE) up -d --build

# === Статус ===

# Проверить контейнеры
ps:
	$(COMPOSE) ps
