# Define variables for better maintainability
COMPOSE_FILE := docker-compose.yml
PROJECT_NAME := houshan  # Replaced space with a hyphen

# Default target
all: up

## Build, (re)create, start, and attach to containers
up:
	@echo "Bringing up the services..."
	sudo docker compose -f $(COMPOSE_FILE) -p $(PROJECT_NAME) up --build --remove-orphans -d

## Stop and remove containers, networks, images, and volumes
down:
	@echo "Stopping and removing the services..."
	sudo docker compose -f $(COMPOSE_FILE) -p $(PROJECT_NAME) down --remove-orphans

## Rebuild and restart the services
rebuild:
	@echo "Rebuilding the services..."
	sudo docker compose -f $(COMPOSE_FILE) -p $(PROJECT_NAME) up --build --force-recreate --remove-orphans

## Stop the running containers (without removing them)
stop:
	@echo "Stopping the running containers..."
	sudo docker compose -f $(COMPOSE_FILE) -p $(PROJECT_NAME) stop

## Show the status of the containers
ps:
	@echo "Displaying the status of the containers..."
	sudo docker compose -f $(COMPOSE_FILE) -p $(PROJECT_NAME) ps

## View the recent logs of the running containers
logs:
	@echo "Displaying the logs of the containers (use Ctrl+C to exit)..."
	sudo docker compose -f $(COMPOSE_FILE) -p $(PROJECT_NAME) logs -f --since 0s

## View the logs of the running containers
logs-all:
	@echo "Displaying the logs of the containers (use Ctrl+C to exit)..."
	sudo docker compose -f $(COMPOSE_FILE) -p $(PROJECT_NAME) logs -f


## Clean up unused Docker resources (volumes, networks, images)
clean:
	@echo "Cleaning up unused Docker resources..."
	sudo docker system prune -af --volumes

## Get shell from app
shell-app:
	@echo "Getting shell from app..."
	sudo docker compose -f $(COMPOSE_FILE) -p $(PROJECT_NAME) exec -it app /bin/bash

## restore data
db-restore:
	@echo "Restore pg_dump data to db..."
	sudo docker compose -f $(COMPOSE_FILE) -p $(PROJECT_NAME) exec -i -T database pg_restore -U root -d houshan --verbose < ./restore/bots.sql

## Help message
help:
	@echo "Available targets:"
	@echo "  up       - Start the services (builds if necessary)"
	@echo "  down     - Stop and remove the services"
	@echo "  rebuild  - Rebuild and restart the services"
	@echo "  stop     - Stop the running containers"
	@echo "  ps       - Show the status of the containers"
	@echo "  logs     - View the logs of the running containers (use -f to follow logs in real-time)"
	@echo "  clean    - Clean up unused Docker resources"

.PHONY: all up down rebuild stop ps logs clean help

# db-init (don't):
# 	aerich init -t src.settings.TORTOISE_ORM

# db-migrate:
# 	aerich migrate --name ""

# db-upgrade:
#   export DATABASE_URL=postgres://root:XXXXX@localhost:54921/houshan
# 	aerich upgrade

# db-copy:
#	pg_dump -h himalayas.liara.cloud -p 30555 -U root -d houshan -F c -t bots | pv > bots.sql