#!/usr/bin/env bash
set -euo pipefail

if [ ! -d "./evibes" ]; then
  printf "\e[31m❌  Please run this script from the project's root (where the 'evibes' directory lives).\e[0m\n"
  exit 1
fi

PURPLE="\e[38;2;121;101;209m"
RESET="\e[0m"
GRAY="\e[90m"
CYAN="\e[36m"

SOURCE="${BASH_SOURCE[0]:-$0}"
SCRIPT_DIR="$(cd "$(dirname "$SOURCE")" && pwd)"
ART_PATH="$SCRIPT_DIR/../ASCII_ART_EVIBES"

if [ ! -f "$ART_PATH" ]; then
  printf "\e[31m❌  Could not find ASCII art at %s\e[0m\n" "$ART_PATH"
  exit 1
fi

while IFS= read -r line; do
  printf "%b%s%b\n" "$PURPLE" "$line" "$RESET"
done < "$ART_PATH"

printf "\n%b                                                                       by WISELESS TEAM%b\n\n" "$GRAY" "$RESET"

printf "%bStopping services…%b\n" "$CYAN" "$RESET"
docker compose down

printf "%bRebuilding services…%b\n" "$CYAN" "$RESET"
docker compose build

printf "%bStarting services…%b\n" "$CYAN" "$RESET"
docker compose up -d

printf "%bApplying database migrations…%b\n" "$CYAN" "$RESET"
docker compose exec app poetry run python manage.py migrate --no-input

printf "%bCollecting static files…%b\n" "$CYAN" "$RESET"
docker compose exec app poetry run python manage.py collectstatic --no-input

printf "%bSetting default caches…%b\n" "$CYAN" "$RESET"
docker compose exec app poetry run python manage.py set_default_caches

printf "%bCleaning up unused Docker data…%b\n" "$CYAN" "$RESET"
docker system prune -f

printf "\n\e[37mAll done! eVibes is up and running.\e[0m\n"
