#!/usr/bin/env bash

set -euo pipefail

ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
FRONTEND_DIR="$ROOT_DIR/frontend"
DEFAULT_HOST="127.0.0.1"
DEFAULT_PORT="3000"

HOST="${1:-$DEFAULT_HOST}"
PORT="${2:-$DEFAULT_PORT}"

cd "$FRONTEND_DIR"

mkdir -p env

if [ ! -f "env/.env.local" ]; then
  cat > "env/.env.local" <<'EOF'
VITE_DIRECTUS_BASE_URL=https://mktcms.ose.tw
VITE_MKTAPI_BASE_URL=http://127.0.0.1:8000
VITE_DIRECTUS_DEBUG_TOKEN=
_API_URL=https://mktcms.ose.tw
_API_TOKEN=
EOF
fi

echo "Starting frontend dev server..."
echo "  host: $HOST"
echo "  port: $PORT"
echo "  env : $FRONTEND_DIR/env/.env.local"

npm run dev -- --host "$HOST" --port "$PORT"

