#!/bin/bash

cat << "EOF"
  ██████╗██╗  ██╗ █████╗ ███╗   ██╗ ██████╗ ███████╗
 ██╔════╝██║  ██║██╔══██╗████╗  ██║██╔════╝ ██╔════╝
 ██║     ███████║███████║██╔██╗ ██║██║  ███╗█████╗  
 ██║     ██╔══██║██╔══██║██║╚██╗██║██║   ██║██╔══╝  
 ╚██████╗██║  ██║██║  ██║██║ ╚████║╚██████╔╝███████╗
  ╚═════╝╚═╝  ╚═╝╚═╝  ╚═╝╚═╝  ╚═══╝ ╚═════╝ ╚══════╝
                                                    
 ███╗   ███╗ █████╗ ██╗  ██╗███████╗██████╗ 
 ████╗ ████║██╔══██╗██║ ██╔╝██╔════╝██╔══██╗
 ██╔████╔██║███████║█████╔╝ █████╗  ██████╔╝
 ██║╚██╔╝██║██╔══██║██╔═██╗ ██╔══╝  ██╔══██╗
 ██║ ╚═╝ ██║██║  ██║██║  ██╗███████╗██║  ██║
 ╚═╝     ╚═╝╚═╝  ╚═╝╚═╝  ╚═╝╚══════╝╚═╝  ╚═╝
                        Reset Wizard
EOF

echo -e "\n\nWelcome to the Changemaker Reset Wizard!\n"
echo "This script will help you reset your Changemaker instance."

# Ask for confirmation
echo -e "\nThis will stop all running containers, delete all files in configs/code-server and configs/ferdium directories"
echo "and reset the .env file to base values."
echo -e "\n⚠️  WARNING: This action cannot be undone! ⚠️"
read -p "Do you want to continue? (y/n): " confirm
if [[ $confirm != [yY]* ]]; then
    echo "Reset canceled."
    exit 0
fi

# Stop all running containers
echo -e "\nStopping all running containers..."
docker compose down
echo "All containers have been stopped."

# Create a timestamp for backups
timestamp=$(date +"%Y%m%d_%H%M%S")

# Backup the .env file
if [ -f .env ]; then
    echo -e "\nCreating backup of .env file..."
    cp .env ".env.backup_$timestamp"
    echo "Backup created as .env.backup_$timestamp"
fi

# Delete specified directories
echo -e "\nDeleting files in configs/code-server and configs/ferdium..."

# Check if directories exist before attempting to delete
if [ -d "configs/code-server" ]; then
    rm -rf configs/code-server/*
    echo "- Cleared configs/code-server/"
else
    mkdir -p configs/code-server
    echo "- Created configs/code-server/ (directory didn't exist)"
fi

if [ -d "configs/ferdium" ]; then
    rm -rf configs/ferdium/*
    echo "- Cleared configs/ferdium/"
else
    mkdir -p configs/ferdium
    echo "- Created configs/ferdium/ (directory didn't exist)"
fi

# Reset the .env file to base values
echo -e "\nResetting .env file to base values..."
cat > .env << 'ENVEOF'
DOMAIN=hello.com

# Listmonk Admin Credentials
LISTMONK_ADMIN_USER=test
LISTMONK_ADMIN_PASSWORD=password

# OpenWebUI Configuration
OPEN_WEBUI_PORT=3005

# Database Credentials
POSTGRES_USER=listmonk
POSTGRES_PASSWORD=password
POSTGRES_DB=listmonk

# Application Configuration
LISTMONK_PORT=9000
LISTMONK_HOSTNAME=listmonk.hello.com

# Monica CRM Configuration
MONICA_APP_KEY=base64:random-key
MONICA_DB_USERNAME=monica
MONICA_DB_PASSWORD=password

# Monica Database Configuration
MONICA_MYSQL_DATABASE=monica
MONICA_MYSQL_USER=monica
MONICA_MYSQL_PASSWORD=password

# MkDocs Configuration
USER_ID=1000
GROUP_ID=1000
MKDOCS_PORT=4000
BASE_DOMAIN=https://hello.com

# Flatnotes Configuration
FLATNOTES_PUID=1000
FLATNOTES_PGID=1000
FLATNOTES_AUTH_TYPE=password
FLATNOTES_USERNAME=test
FLATNOTES_PASSWORD=test
FLATNOTES_SECRET_KEY=random-secret-key
FLATNOTES_PORT=8089

# Gitea Configuration
GITEA_DB_TYPE=mysql
GITEA_DB_HOST=gitea-db:3306
GITEA_DB_NAME=gitea
GITEA_DB_USER=gitea
GITEA_DB_PASSWD=password
GITEA_DB_ROOT_PASSWORD=password
GITEA_WEB_PORT=3030
GITEA_SSH_PORT=2225
GITEA_ROOT_URL=https://gitea.hello.com
GITEA_DOMAIN=gitea.hello.com

# Apache Answer Configuration
ANSWER_APP_PORT=9080

# Excalidraw Configuration
EXCALIDRAW_PORT=3333
EXCALIDRAW_LIBRARY_URL=https://libraries.excalidraw.com
EXCALIDRAW_LIBRARY_BACKEND=https://us-central1-excalidraw-room-persistence.cloudfunctions.net/libraries
# For Cloudflare Tunnel - update these values with your Cloudflare Tunnel domain
EXCALIDRAW_PUBLIC_URL=https://excalidraw.hello.com
EXCALIDRAW_PUBLIC_SOCKET_URL=https://excalidraw.hello.com

# Code Server Configuration
CODE_SERVER_PORT=8888
USER_NAME=coder

# Cloudflare Credentials - Replace with your actual values
CF_AUTH_EMAIL=your_cloudflare_email@example.com
CF_AUTH_KEY=your_cloudflare_global_api_key
CF_ZONE_ID=your_cloudflare_zone_id
CF_TUNNEL_ID=your_cloudflared_tunnel_id
CF_DOMAIN=yourdomain.com

# NocoDB Configuration
NOCODB_PORT=8090
NOCODB_JWT_SECRET=replace-with-secure-random-string
NOCODB_DB_NAME=nocodb
NOCODB_DB_USER=noco
NOCODB_DB_PASSWORD=password
# Connection string format: pg://nocodb-db:5432?u=noco&p=password&d=nocodb

# n8n Configuration
N8N_PORT=5678
N8N_HOST=n8n.hello.com
GENERIC_TIMEZONE=UTC
N8N_ENCRYPTION_KEY=change-me-please
N8N_USER_EMAIL=admin@example.com
N8N_USER_PASSWORD=changeMe

# ConvertX Configuration
CONVERTX_PORT=3100
CONVERTX_JWT_SECRET=aLongAndSecretStringUsedToSignTheJSONWebToken1234
ENVEOF

echo -e "\n✅ Reset completed successfully!"
echo "The following actions were performed:"
echo "  - All Docker containers were stopped"
echo "  - Directories configs/code-server and configs/ferdium were cleared"
echo "  - .env file was reset to base values"
echo "  - A backup of your original .env file was created (if it existed)"
echo -e "\nYour Changemaker instance is now reset to default settings."
echo "You can restart your services with 'docker compose up -d' when ready."
