#!/bin/bash
# =============================================================
#  Dental Manager — Setup Steps 5–13
#  Run this from a terminal where you can enter your sudo password
# =============================================================

set -e   # stop on any error
cd "$(dirname "$0")"   # always run from the project root

echo ""
echo "=============================="
echo " STEP 5 — Install PostgreSQL  "
echo "=============================="
sudo apt-get install -y postgresql postgresql-contrib
sudo systemctl enable postgresql
sudo systemctl start postgresql
sudo -u postgres psql -c "CREATE DATABASE dentalapp OWNER postgres;" 2>/dev/null || echo "(database already exists, skipping)"
sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD 'mypassword';"
sudo sed -i 's/scram-sha-256/md5/g' /etc/postgresql/*/main/pg_hba.conf
sudo systemctl restart postgresql
echo "✓ PostgreSQL ready"

echo ""
echo "=============================="
echo " STEP 6 — Install Redis       "
echo "=============================="
sudo apt-get install -y redis-server
sudo systemctl enable redis-server
sudo systemctl start redis-server
redis-cli ping | grep -q PONG && echo "✓ Redis ready" || echo "⚠ Redis ping failed"

echo ""
echo "=============================="
echo " STEP 7 — npm install         "
echo "=============================="
npm install
echo "✓ Node.js dependencies installed (Python venvs created via postinstall)"

echo ""
echo "=============================="
echo " STEP 9 — Setup .env files    "
echo "=============================="
npm run setup:env
echo "✓ .env files created"
echo ""
echo ">>> STEP 9a: Cloudflare subdomain skipped."
echo "    When ready, open apps/Frontend/.env and apps/Backend/.env"
echo "    and replace 'yoursubdomain' with the office subdomain."

echo ""
echo "=============================="
echo " STEP 10 — Database setup     "
echo "=============================="
npm run db:migrate
npm run db:generate
npm run db:seed
echo "✓ Database migrated, types generated, and seeded"

echo ""
echo "=============================="
echo " STEP 11 — Configure nginx    "
echo "=============================="
sudo apt-get install -y nginx
sudo cp nginx.conf /etc/nginx/sites-available/dental-app
sudo ln -sf /etc/nginx/sites-available/dental-app /etc/nginx/sites-enabled/dental-app
sudo rm -f /etc/nginx/sites-enabled/default
sudo nginx -t && sudo systemctl reload nginx
echo "✓ nginx configured"

echo ""
echo "=============================="
echo " STEP 13 — Desktop shortcuts  "
echo "=============================="
bash ~/Desktop/DentalManagementMH06/setup-desktop-shortcut.sh
echo "✓ Desktop shortcuts created"

echo ""
echo "============================================"
echo " All steps complete!"
echo ""
echo " STEP 12 — To run the app, open 2 terminals:"
echo "   Terminal 1:  cd ~/Desktop/DentalManagementMH06 && npm run dev"
echo "   Terminal 2:  cd ~/Desktop/DentalManagementMH06/apps/SeleniumService && .venv/bin/python3 agent.py"
echo "============================================"
