# AutoRia Telegram Bot - Docker Management
# =====================================

.PHONY: help build up down restart logs status clean setup health test debug

# Default target
help:
	@echo "🐳 AutoRia Telegram Bot - Docker Commands"
	@echo "=========================================="
	@echo ""
	@echo "📦 Setup and Build:"
	@echo "  setup     - Initial setup (create directories, copy env file)"
	@echo "  build     - Build Docker image"
	@echo "  rebuild   - Build Docker image without cache"
	@echo ""
	@echo "🚀 Container Management:"
	@echo "  up        - Start the bot in background"
	@echo "  down      - Stop and remove containers"
	@echo "  restart   - Restart the bot"
	@echo "  stop      - Stop the bot"
	@echo ""
	@echo "📊 Monitoring:"
	@echo "  logs      - Show live logs"
	@echo "  status    - Show container status"
	@echo "  health    - Check bot health"
	@echo "  stats     - Show resource usage"
	@echo ""
	@echo "🔧 Development:"
	@echo "  debug     - Run bot in debug mode"
	@echo "  shell     - Open shell in container"
	@echo "  test      - Run health tests"
	@echo ""
	@echo "🧹 Cleanup:"
	@echo "  clean     - Remove containers and images"
	@echo "  clean-all - Remove everything including volumes"

# Setup commands
setup:
	@echo "🔧 Setting up AutoRia Bot environment..."
	@mkdir -p logs temp
	@if [ ! -f .env ]; then \
		cp env.example .env; \
		echo "📋 Created .env file from template"; \
		echo "⚠️  Please edit .env file with your credentials!"; \
	else \
		echo "📋 .env file already exists"; \
	fi
	@if [ ! -f database.db ]; then \
		touch database.db; \
		chmod 644 database.db; \
		echo "💾 Created database.db file"; \
	fi
	@echo "✅ Setup completed!"

# Build commands
build:
	@echo "🏗️  Building AutoRia Bot Docker image..."
	docker-compose build

rebuild:
	@echo "🏗️  Rebuilding AutoRia Bot Docker image (no cache)..."
	docker-compose build --no-cache

# Container management
up: setup
	@echo "🚀 Starting AutoRia Bot..."
	docker-compose up -d
	@echo "✅ Bot started successfully!"
	@echo "📝 Use 'make logs' to view logs"

down:
	@echo "🛑 Stopping AutoRia Bot..."
	docker-compose down
	@echo "✅ Bot stopped successfully!"

restart:
	@echo "🔄 Restarting AutoRia Bot..."
	docker-compose restart
	@echo "✅ Bot restarted successfully!"

stop:
	@echo "⏸️  Stopping AutoRia Bot..."
	docker-compose stop
	@echo "✅ Bot stopped successfully!"

# Monitoring
logs:
	@echo "📝 Showing AutoRia Bot logs (Ctrl+C to exit)..."
	docker-compose logs -f autoria-bot

status:
	@echo "📊 AutoRia Bot Container Status:"
	@echo "================================"
	docker-compose ps

health:
	@echo "🏥 Checking AutoRia Bot health..."
	@echo "================================"
	@if docker-compose ps | grep -q "autoria_telegram_bot.*Up"; then \
		echo "✅ Container is running"; \
		docker exec autoria_telegram_bot python -c "import requests; print('✅ Internet connection OK' if requests.get('https://api.telegram.org', timeout=10).status_code == 200 else '❌ Internet connection FAILED')" 2>/dev/null || echo "❌ Health check failed"; \
		docker exec autoria_telegram_bot google-chrome --version 2>/dev/null | head -1 || echo "❌ Chrome not available"; \
		docker exec autoria_telegram_bot chromedriver --version 2>/dev/null | head -1 || echo "❌ ChromeDriver not available"; \
	else \
		echo "❌ Container is not running"; \
	fi

stats:
	@echo "📈 AutoRia Bot Resource Usage:"
	@echo "=============================="
	docker stats autoria_telegram_bot --no-stream

# Development
debug:
	@echo "🐛 Starting AutoRia Bot in debug mode..."
	docker-compose run --rm -e LOG_LEVEL=DEBUG autoria-bot python run.py

shell:
	@echo "🐚 Opening shell in AutoRia Bot container..."
	docker exec -it autoria_telegram_bot bash

test:
	@echo "🧪 Running AutoRia Bot tests..."
	@echo "==============================="
	@echo "1. Testing Selenium..."
	@docker exec autoria_telegram_bot python -c "\
from selenium import webdriver; \
from selenium.webdriver.chrome.options import Options; \
options = Options(); \
options.add_argument('--headless'); \
options.add_argument('--no-sandbox'); \
options.add_argument('--disable-dev-shm-usage'); \
driver = webdriver.Chrome(options=options); \
driver.get('https://google.com'); \
print('✅ Selenium test passed:', driver.title[:50]); \
driver.quit()" 2>/dev/null || echo "❌ Selenium test failed"
	@echo ""
	@echo "2. Testing database connection..."
	@docker exec autoria_telegram_bot python -c "\
import sqlite3; \
conn = sqlite3.connect('database.db'); \
cursor = conn.cursor(); \
cursor.execute('SELECT 1'); \
print('✅ Database connection OK'); \
conn.close()" 2>/dev/null || echo "❌ Database test failed"
	@echo ""
	@echo "3. Testing Python imports..."
	@docker exec autoria_telegram_bot python -c "\
import aiogram, selenium, requests, sqlalchemy; \
print('✅ All main imports OK')" 2>/dev/null || echo "❌ Import test failed"

# Cleanup
clean:
	@echo "🧹 Cleaning up AutoRia Bot containers and images..."
	docker-compose down --rmi local --remove-orphans
	@echo "✅ Cleanup completed!"

clean-all:
	@echo "🧹 Cleaning up everything (including volumes)..."
	@read -p "⚠️  This will remove ALL data including database! Continue? (y/N): " confirm && [ "$$confirm" = "y" ]
	docker-compose down -v --rmi all --remove-orphans
	docker system prune -f
	@echo "✅ Full cleanup completed!"

# Development shortcuts
dev-up: rebuild up logs

dev-restart: down up logs

quick-logs:
	docker-compose logs --tail=50 autoria-bot

# Update and deploy
update:
	@echo "🔄 Updating AutoRia Bot..."
	git pull
	make rebuild
	make up
	@echo "✅ Update completed!" 