#!/usr/bin/env bash
#
# Domainsansar local setup.
#
# Prepares the single unified Laravel + React app for local development.
# Idempotent: safe to re-run after pulling changes.

set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT"

info()  { printf '\033[1;34m==>\033[0m %s\n' "$1"; }
warn()  { printf '\033[1;33m!!\033[0m %s\n' "$1"; }
fail()  { printf '\033[1;31mxx\033[0m %s\n' "$1" >&2; exit 1; }

# ---------------------------------------------------------------------------
# Prerequisites
# ---------------------------------------------------------------------------
command -v php >/dev/null      || fail 'php is not installed.'
command -v composer >/dev/null || fail 'composer is not installed.'
command -v node >/dev/null     || fail 'node is not installed.'

php -r 'exit(version_compare(PHP_VERSION, "8.3", ">=") ? 0 : 1);' \
  || fail "PHP 8.3 or newer is required; found $(php -r 'echo PHP_VERSION;')."

for ext in pdo_mysql mbstring intl; do
  php -m | grep -qx "$ext" || fail "The PHP extension [$ext] is required."
done

# ---------------------------------------------------------------------------
# PHP Dependencies & Env
# ---------------------------------------------------------------------------
info 'Installing PHP dependencies'
composer install --no-interaction

if [ ! -f .env ]; then
  info 'Creating .env'
  cp .env.example .env
  php artisan key:generate --ansi
else
  info '.env already exists, leaving it alone'
fi

info 'Checking the database connection'
if php artisan db:show --json >/dev/null 2>&1; then
  info 'Running migrations'
  php artisan migrate --force

  info 'Seeding marketplace reference data (TLDs, categories, plans, fees)'
  php artisan db:seed --force
else
  warn 'Could not connect to the database.'
  warn 'Create it and the application user first:'
  warn '    sudo mysql < scripts/setup-database.sql'
  warn 'Then set DB_PASSWORD in .env and re-run this script.'
fi

info 'Linking storage'
php artisan storage:link 2>/dev/null || true

# ---------------------------------------------------------------------------
# JS Dependencies & Assets
# ---------------------------------------------------------------------------
info 'Installing JS dependencies'
npm install --no-audit --no-fund

cat <<'DONE'

Setup complete.

  Application  php artisan serve (App + API served together on :8000)
  Vite Dev     npm run dev (Optional: for HMR during UI development)

  Queue        php artisan queue:work
               Login risk assessment and the maintenance jobs run here.

  Scheduler    php artisan schedule:work
               Four tasks: three retention sweeps and a nightly fee-schedule
               integrity check. `php artisan schedule:list` shows them.

Checks

  Backend tests            composer test
  Backend tests on MySQL   ./scripts/test-mysql.sh
  Frontend tests           npm test
  Code style               composer lint
  Fee schedule sanity      composer fees:check
  Schema portability       composer schema:check

DONE
