#!/usr/bin/env bash
# First-time install on Ubuntu/Debian VPS
# Usage: sudo bash scripts/deploy/vps-install.sh
set -euo pipefail

APP_DIR="${APP_DIR:-/var/www/saas-accounting}"
APP_USER="${APP_USER:-www-data}"
APP_URL="${APP_URL:-https://test.duiclick.com}"
REPO_URL="${REPO_URL:-https://github.com/mhr01828/Laravel-11-Project.git}"
BRANCH="${BRANCH:-main}"
PHP_VERSION="${PHP_VERSION:-8.3}"

echo "== AXIS SaaS Accounting — VPS first-time install =="
echo "App dir: ${APP_DIR}"
echo "URL:     ${APP_URL}"

if [[ "${EUID}" -ne 0 ]]; then
  echo "Run as root: sudo bash scripts/deploy/vps-install.sh"
  exit 1
fi

apt-get update -qq
apt-get install -y -qq \
  "php${PHP_VERSION}" "php${PHP_VERSION}-fpm" "php${PHP_VERSION}-mysql" \
  "php${PHP_VERSION}-mbstring" "php${PHP_VERSION}-xml" "php${PHP_VERSION}-curl" \
  "php${PHP_VERSION}-zip" "php${PHP_VERSION}-gd" "php${PHP_VERSION}-bcmath" \
  "php${PHP_VERSION}-intl" nginx mysql-server git unzip curl

if [[ ! -d "${APP_DIR}/.git" ]]; then
  mkdir -p "$(dirname "${APP_DIR}")"
  git clone --branch "${BRANCH}" "${REPO_URL}" "${APP_DIR}"
else
  echo "Repo already exists at ${APP_DIR}, pulling latest..."
  git -C "${APP_DIR}" pull origin "${BRANCH}"
fi

cd "${APP_DIR}"

if [[ ! -f .env ]]; then
  cp .env.example .env
  sed -i "s|APP_URL=.*|APP_URL=${APP_URL}|" .env
  sed -i 's|APP_ENV=.*|APP_ENV=production|' .env
  sed -i 's|APP_DEBUG=.*|APP_DEBUG=false|' .env
  echo ""
  echo "Created .env — EDIT database credentials before continuing:"
  echo "  nano ${APP_DIR}/.env"
  echo ""
  read -r -p "Press Enter after you saved .env (DB_* settings)..." _
fi

mkdir -p storage/framework/{cache,sessions,views} storage/logs bootstrap/cache
chown -R "${APP_USER}:${APP_USER}" storage bootstrap/cache
chmod -R 775 storage bootstrap/cache

php artisan key:generate --force
php artisan storage:link || true
php artisan migrate --seed --force
php artisan config:cache
php artisan route:cache
php artisan view:cache

NGINX_SITE="/etc/nginx/sites-available/saas-accounting"
cat > "${NGINX_SITE}" <<EOF
server {
    listen 80;
    server_name test.duiclick.com;
    root ${APP_DIR}/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;
    charset utf-8;

    location / {
        try_files \$uri \$uri/ /index.php?\$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php${PHP_VERSION}-fpm.sock;
        fastcgi_param SCRIPT_FILENAME \$realpath_root\$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_hide_header X-Powered-By;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
EOF

ln -sf "${NGINX_SITE}" /etc/nginx/sites-enabled/saas-accounting
rm -f /etc/nginx/sites-enabled/default
nginx -t
systemctl reload nginx
systemctl enable "php${PHP_VERSION}-fpm" nginx mysql

echo ""
echo "== Install complete =="
echo "Site: ${APP_URL}"
echo "App:  ${APP_DIR}"
echo ""
echo "Next (recommended):"
echo "  certbot --nginx -d test.duiclick.com"
echo ""
echo "Demo login: DEMO / admin / Demo@1234"
echo "Super admin: ${APP_URL}/sadmin — admin@saasaccounting.com / Admin@1234"
